0

Hi I'm new to GUI programming, so after trying PyQt for a short time I found Enaml which made the production much more easy.

I'm trying to have a widget that can change the value of a datetime.datetime object or a datetime.time object, but it turns out they are read-only. So how could I do that? Can I change the variable to have read-and-write attributes, or maybe need a setter?

My minimum working solution is:

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()
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
smileynor
  • 1
  • 4

2 Answers2

1

Your solution is incorrect. You use the member types to create members in your Atom class which can then be accessed and set through the normal __getattr__ mechanism.

A correct example of your sample code:

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()
Griffin
  • 13,184
  • 4
  • 29
  • 43
0

I found a solution based on looking into the Property.py documentation of Nucleic Development Team Atom. The setter could be done by adding a function with _set_variable(self,variable) and _get_variable(self): A possible solution is therefore:

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()
smileynor
  • 1
  • 4