您好我是GUI编程的新手,所以在尝试PyQt很短的时间后,我发现Enaml让生产变得更加容易。
我正在尝试使用可以更改datetime.datetime对象或datetime.time对象的值的窗口小部件,但事实证明它们是只读的。那我该怎么办呢?我可以将变量更改为具有读写属性,还是需要setter?
我的最低工作解决方案是:
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()
答案 0 :(得分: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()
答案 1 :(得分:0)
我找到了一个基于查看Nucleic Development Team Atom的Property.py文档的解决方案。 可以通过添加 _set_variable(self,variable)和 _get_variable(self):的函数来完成setter 因此,可能的解决方案是:
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()