]>
Commit | Line | Data |
---|---|---|
1e4a197e RD |
1 | from wxPython.wx import * |
2 | from wxPython.lib.popupctl import wxPopupControl | |
3 | from wxPython.calendar import * | |
4 | ||
5 | class TestDateControl(wxPopupControl): | |
6 | def __init__(self,*_args,**_kwargs): | |
7 | apply(wxPopupControl.__init__,(self,) + _args,_kwargs) | |
8 | ||
9 | self.win = wxWindow(self,-1,pos = (0,0),style = 0) | |
10 | self.cal = wxCalendarCtrl(self.win,-1,pos = (0,0)) | |
11 | ||
12 | bz = self.cal.GetBestSize() | |
13 | self.win.SetSize(bz) | |
14 | ||
15 | # This method is needed to set the contents that will be displayed | |
16 | # in the popup | |
17 | self.SetPopupContent(self.win) | |
18 | ||
19 | # Event registration for date selection | |
20 | EVT_CALENDAR_DAY(self.cal,self.cal.GetId(),self.OnCalSelected) | |
21 | ||
22 | # Method called when a day is selected in the calendar | |
23 | def OnCalSelected(self,evt): | |
24 | self.PopDown() | |
25 | date = self.cal.GetDate() | |
26 | ||
27 | # Format the date that was selected for the text part of the control | |
28 | self.SetValue('%02d/%02d/%04d' % (date.GetDay(), | |
29 | date.GetMonth()+1, | |
30 | date.GetYear())) | |
31 | evt.Skip() | |
32 | ||
33 | # Method overridden from wxPopupControl | |
34 | # This method is called just before the popup is displayed | |
35 | # Use this method to format any controls in the popup | |
36 | def FormatContent(self): | |
37 | # I parse the value in the text part to resemble the correct date in | |
38 | # the calendar control | |
39 | txtValue = self.GetValue() | |
40 | dmy = txtValue.split('/') | |
41 | didSet = False | |
42 | if len(dmy) == 3: | |
43 | date = self.cal.GetDate() | |
44 | d = int(dmy[0]) | |
45 | m = int(dmy[1]) - 1 | |
46 | y = int(dmy[2]) | |
47 | if d > 0 and d < 31: | |
48 | if m >= 0 and m < 12: | |
49 | if y > 1000: | |
50 | self.cal.SetDate(wxDateTimeFromDMY(d,m,y)) | |
51 | didSet = True | |
52 | if not didSet: | |
53 | self.cal.SetDate(wxDateTime_Today()) | |
54 | ||
55 | ||
56 | #--------------------------------------------------------------------------- | |
57 | ||
58 | class TestPanel(wxPanel): | |
59 | def __init__(self, parent, log): | |
60 | self.log = log | |
61 | wxPanel.__init__(self, parent, -1) | |
62 | date = TestDateControl(self, -1, pos = (30,30), size = (100,22)) | |
63 | ||
64 | #---------------------------------------------------------------------- | |
65 | ||
66 | def runTest(frame, nb, log): | |
67 | win = TestPanel(nb, log) | |
68 | return win | |
69 | ||
70 | #---------------------------------------------------------------------- | |
71 | ||
72 | ||
73 | ||
74 | overview = """<html><body> | |
75 | <h2><center>wxPopupControl</center></h2> | |
76 | ||
77 | wxPopupControl is a class that can display a value and has a button | |
78 | that will popup another window similar to how a wxComboBox works. The | |
79 | popup window can contain whatever is needed to edit the value. This | |
80 | example uses a wxCalendarCtrl. | |
81 | ||
82 | <p>Currently a wxDialog is used for the popup. Eventually a | |
83 | wxPopupWindow should be used... | |
84 | ||
85 | </body></html> | |
86 | """ | |
87 | ||
88 | ||
89 | ||
90 | if __name__ == '__main__': | |
91 | import sys,os | |
92 | import run | |
93 | run.main(['', os.path.basename(sys.argv[0])]) | |
94 |