大家好,我是图形用户界面编程的新手,所以在尝试了一小段时间后,我发现了PyQt,它使生产变得更加容易。
我正在尝试一个可以更改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()发布于 2016-05-26 01:44:17
您的解决方案不正确。您可以使用成员类型在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-04-17 17:57:52
通过查阅Nucleic Development Team Atom的Property.py文档,我找到了一个解决方案。可以通过使用_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()https://stackoverflow.com/questions/36666313
复制相似问题