]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | ||
3 | from wxPython.wx import * | |
4 | ||
5 | class FieldData: | |
6 | def __init__(self, name, label, shortHelp="", defValue="", | |
7 | size=(-1, -1), style=0, ID=-1): | |
8 | self.name = name | |
9 | self.label = label | |
10 | self.shortHelp = shortHelp | |
11 | self.defValue = defValue | |
12 | self.size = size | |
13 | self.style = style | |
14 | self.ID = ID | |
15 | ||
16 | ||
17 | class DynamicForm(wxPanel): | |
18 | def __init__(self, parent, ID, fieldData): | |
19 | wxPanel.__init__(self, parent, ID) | |
20 | ||
21 | sizer = wxFlexGridSizer(cols=2, vgap=5, hgap=5) | |
22 | for field in fieldData: | |
23 | label = wxStaticText(self, -1, field.label) | |
24 | sizer.Add(label, 0, wxALIGN_RIGHT) | |
25 | text = wxTextCtrl(self, field.ID, field.defValue, | |
26 | size=field.size, style=field.style) | |
27 | if field.shortHelp: | |
28 | text.SetToolTip(wxToolTip(field.shortHelp)) | |
29 | self.__dict__["get_"+field.name] = text.GetValue | |
30 | self.__dict__["set_"+field.name] = text.SetValue | |
31 | sizer.Add(text, 0, wxEXPAND) | |
32 | ||
33 | sizer.Fit(self) | |
34 | self.SetAutoLayout(true) | |
35 | self.SetSizer(sizer) | |
36 | ||
37 | ||
38 | class TestFrame(wxFrame): | |
39 | testFields = [ | |
40 | FieldData("fname", "First name:", "Enter someone's first name"), | |
41 | FieldData("lname", "Last name:", "Enter someone's last name"), | |
42 | FieldData("email", "Email address:", "just figure it out..."), | |
43 | ] | |
44 | def __init__(self): | |
45 | wxFrame.__init__(self, None, -1, "This is a test") | |
46 | form = DynamicForm(self, -1, self.testFields) | |
47 | form.set_fname("Robin") | |
48 | form.set_lname("Dunn") | |
49 | self.form = form | |
50 | self.Fit() | |
51 | EVT_CLOSE(self, self.OnCloseWindow) | |
52 | ||
53 | def OnCloseWindow(self, evt): | |
54 | print self.form.get_email() | |
55 | self.Destroy() | |
56 | ||
57 | ||
58 | app = wxPySimpleApp() | |
59 | frame = TestFrame() | |
60 | frame.Show(true) | |
61 | app.MainLoop() | |
62 |