Greetings-
I am attempting to make my Date/Time dialog a class rather than a function.
I have attempted to cobble together code from the demo, but get the
following error when I attempt to run the code:
Traceback (most recent call last):
File "C:/WINNT/Profiles/glinds/Desktop/GUS/DateTimeDialog.py", line 133,
in OnTest1
r = DateTimeDialog(self.f)
File "C:/WINNT/Profiles/glinds/Desktop/GUS/DateTimeDialog.py", line 20, in
__init__
wxDefaultPosition, wxSize(350,200))
TypeError: unbound method __init__() must be called with instance as first
argument
Here is the code, thanks for your help.
from wxPython.wx import *
from wxPython.calendar import *
from wxPython.utils import *
CURRENT_VERSION = '0.2'
ID_CALENDAR = NewId()
ID_HOUR = NewId()
ID_MINUTE = NewId()
ID_SECOND = NewId()
class DateTimeDialog(wxDialog):
def __init__(self, parent, id=-1, default=None):
"""
A dialog box to enter date/time values. GetValue() returns a
wxDateTime object.
"""
wxDialog.__init__(parent, id,
'Date/Time Dialog v%s' % CURRENT_VERSION,
wxDefaultPosition, wxSize(350,200))
# get current system date/time
now = wxDateTime()
now.SetToCurrent()
# set the default values to the current system date/time
default_hour = now.GetHour()
default_minute = now.GetMinute()
default_second = now.GetSecond()
# if another date/time was passed in (and is a wxDateTime), we use
it
if default and type(default) == type(now):
now = default
# create a date/time object for the return value and set it to
# the value we will initially display
self.return_value = wxDateTime()
self.return_value = now
# create/set the calendar
self.calendar = wxCalendarCtrl(self, ID_CALENDAR, now, pos=(10,50),
style=wxCAL_SHOW_HOLIDAYS | wxCAL_SUNDAY_FIRST)
EVT_CALENDAR_SEL_CHANGED(self, ID_CALENDAR, self.OnDateChange)
time_x = 200 # the x coordinate for the first (Hour) time spinner
time_dx = 45 # distance between the time spinners (x direction)
time_y = 25 # the y coordinate for all time spinners
# hour
wxStaticText(self, -1, 'Hour', wxPoint(time_x, time_y-15))
self.HH = wxSpinCtrl(self, ID_HOUR, '', wxPoint(time_x,time_y),
wxSize(40,-1))
self.HH.SetRange(0,23)
self.HH.SetValue(now.GetHour())
EVT_SPINCTRL(self, ID_HOUR, self.OnTimeChange)
# minute
wxStaticText(self, -1, 'Minute', wxPoint(time_x+time_dx, time_y-15))
self.MM = wxSpinCtrl(self, ID_MINUTE, '',
wxPoint(time_x+time_dx,time_y),
wxSize(40,-1))
self.MM.SetRange(0,59)
self.MM.SetValue(now.GetMinute())
EVT_SPINCTRL(self, ID_MINUTE, self.OnTimeChange)
# second
wxStaticText(self, -1, 'Second', wxPoint(time_x+2*time_dx,
time_y-15))
self.SS = wxSpinCtrl(self, -1, '',wxPoint(time_x+2*time_dx,time_y),
wxSize(40,-1))
self.SS.SetRange(0,59)
self.SS.SetValue(now.GetSecond())
EVT_SPINCRTL(self, ID_SECOND, self.OnTimeChange)
# OK/Cancel buttons
bx = 260
by = 110
wxButton(self,wxID_OK,' OK ',wxPoint(bx,
by),wxDefaultSize).SetDefault()
wxButton(self, wxID_CANCEL, ' Cancel ',wxPoint(bx,
by+30),wxDefaultSize)
def OnDateChange(self, evt):
self.return_value.SetYear(self.calendar.GetYear())
self.return_value.SetMonth(self.calendar.GetMonth())
self.return_value.SetDay(self.calendar.GetDay())
def OnTimeChange(self, evt):
self.return_value.SetHour(self.HH.GetValue())
self.return_value.SetMinute(self.MM.GetValue())
self.return_value.SetSecond(self.SS.GetValue())
def GetValue(self):
return self.return_value
···
############################################################################
##
### <Test Code>
##############################################################
############################################################################
##
if __name__ == '__main__':
ID_TEST_1 = NewId()
ID_TEST_2 = NewId()
ID_TEST_3 = NewId()
class myApp(wxApp):
def OnInit(self):
self.f = frame = wxFrame(NULL, -1, 'Date/Time Unit test',
wxPoint(-1,-1), wxSize(500, 200))
frame.Show(true)
self.SetTopWindow(frame)
panel = wxPanel(frame, -1)
# Test 1 - Get a date without supplying a default
wxStaticText(panel, -1, 'Set a date (current default):',
wxPoint(10, 20))
EVT_BUTTON(self, ID_TEST_1, self.OnTest1)
b1 = wxButton(panel, ID_TEST_1, 'Run Test!', wxPoint(150, 20))
self.r1 = wxTextCtrl(panel,-1,'',wxPoint(250,20),wxSize(220,-1))
# Test 2 - Get a date (supply a default value)
wxStaticText(panel, -1, 'Set a date (my default):',
wxPoint(10, 60))
EVT_BUTTON(self, ID_TEST_2, self.OnTest2)
b2 = wxButton(panel, ID_TEST_2, 'Run Test!', wxPoint(150, 60))
self.r2 = wxTextCtrl(panel,-1,'',wxPoint(250,60),wxSize(220,-1))
# Test 3 - Get a date (supply a bad default value)
wxStaticText(panel, -1, 'Set a date (bad default):',
wxPoint(10, 100))
EVT_BUTTON(self, ID_TEST_3, self.OnTest3)
b3 = wxButton(panel, ID_TEST_3, 'Run Test!', wxPoint(150, 100))
self.r3 =
wxTextCtrl(panel,-1,'',wxPoint(250,100),wxSize(220,-1))
return true
def OnTest1(self, evt):
r = DateTimeDialog(self.f)
if r.ShowModal() == wxID_OK:
self.r1.SetValue(self.format_date(r))
def OnTest2(self, evt):
pass
def OnTest3(self, evt):
pass
def format_date(self, d):
return '%s/%s/%s %s:%s:%s' %(
1+d.GetMonth(), d.GetDay(), d.GetYear(),
d.GetHour(), d.GetMinute(), d.GetSecond())
app = myApp(0)
app.MainLoop()