0

嗨,我是 GUI 编程的新手,所以在短暂尝试 PyQt 后,我​​发现 Enaml 使生产变得更加容易。

我正在尝试拥有一个可以更改 datetime.datetime 对象或 datetime.time 对象的值的小部件,但事实证明它们是只读的。那我怎么能这样做呢?我可以将变量更改为具有读写属性,或者可能需要一个设置器吗?

我的最低工作解决方案是:

from __future__ import print_function
import datetime
import os

from atom.api import Atom, Unicode, Range, Typed, observe, Value, Bool

class SimulationCase(Atom):

    startDateTime = datetime.datetime.strptime('05.03.2015-5','%d.%m.%Y-%H')
    currentDateTime = startDateTime
    endDateTime = startDateTime+ datetime.timedelta(days=int(5))
    incrementTime = datetime.time(1,0,0)

def main():
    case = SimulationCase()

    print(case.currentDateTime)

    a = datetime.time(1,0,0)
    print(a)

    #This is where the problem occures, comment out the line under for a working solution.
    case.incrementTime = a


if __name__ == '__main__':
    main()
4

2 回答 2

1

您的解决方案不正确。您可以使用成员类型在 Atom 类中创建成员,然后可以通过正常__getattr__机制访问和设置这些成员。

您的示例代码的正确示例:

from __future__ import print_function
import datetime
import os

from atom.api import Atom, Unicode, Range, Typed, observe, Value, Bool

def default_time_factory():
    return datetime.datetime.strptime('05.03.2015-5','%d.%m.%Y-%H')

class SimulationCase(Atom):

    startDateTime   = Typed( datetime.datetime, factory=default_time_factory )
    currentDateTime = Typed( datetime.datetime, factory=default_time_factory )
    endDateTime     = Typed( datetime.datetime, default=default_time_factory()+datetime.timedelta(days=int5) )
    incrementTime   = Typed( datetime.time,     default=datetime.time(1,0,0) )

def main():
    case = SimulationCase()

    print(case.currentDateTime)

    a = datetime.time(1,0,0)
    print(a)

    #This now works
    case.incrementTime = a


if __name__ == '__main__':
    main()
于 2016-05-25T17:44:16.850 回答
0

我通过查看 Nucleic Development Team Atom 的 Property.py 文档找到了一个解决方案。setter 可以通过添加一个带有_set_variable(self,variable)_get_variable(self) 的函数来完成: 因此,一个可能的解决方案是:

from __future__ import print_function
import datetime
import os

from atom.api import Atom, Unicode, Range, Typed, observe, Value, Bool, Property

class SimulationCase(Atom):

    startDateTime = Property()
    _startDateTime = Typed(datetime.datetime)

    currentDateTime = Property()
    _currentDateTime = Typed(datetime.datetime)

    endDateTime = Property()
    _endDateTime = Typed(datetime.datetime)

    incrementTime = Property()
    _incrementTime = Typed(datetime.time)

    # Getter and setter for startDateTime
    def _set_startDateTime(self,startDateTime):
        self._startDateTime = startDateTime

    def _get_startDateTime(self):
        return self._startDateTime

    # Getter and setter for currentDateTime
    def _set_currentDateTime(self,currentDateTime):
        self._currentDateTime = currentDateTime

    def _get_currentDateTime(self):
        return self._currentDateTime

    # Getter and setter for endDateTime
    def _set_endDateTime(self,endDateTime):
        self._endDateTime = endDateTime

    def _get_endDateTime(self):
        return self._endDateTime

    # Getter and setter for incrementTime
    def _set_incrementTime(self,incrementTime):
        self._incrementTime = incrementTime

    def _get_incrementTime(self):
        return self._incrementTime

    # Populating the data
    def __init__(self):
        self._startDateTime = datetime.datetime.strptime('05.03.2015-5','%d.%m.%Y-%H')
        self._currentDateTime = self._startDateTime
        self._endDateTime = self._startDateTime + datetime.timedelta(days=int(5))
        self._incrementTime = datetime.time(1,0,0)



def main():
    case = SimulationCase()

    print(case.currentDateTime)
    print(case.incrementTime)
    print(case.endDateTime)

    a = datetime.time(2,0,0)

    case.incrementTime = a
    print(case.incrementTime)



if __name__ == '__main__':
    main()
于 2016-04-17T09:57:52.063 回答