]>
Commit | Line | Data |
---|---|---|
cf694132 | 1 | |
8fa876ca | 2 | import wx |
cf694132 RD |
3 | |
4 | #--------------------------------------------------------------------------- | |
1e4a197e RD |
5 | # Create and set a help provider. Normally you would do this in |
6 | # the app's OnInit as it must be done before any SetHelpText calls. | |
8fa876ca RD |
7 | provider = wx.SimpleHelpProvider() |
8 | wx.HelpProvider_Set(provider) | |
031787de | 9 | |
1e4a197e RD |
10 | #--------------------------------------------------------------------------- |
11 | ||
8fa876ca RD |
12 | class TestDialog(wx.Dialog): |
13 | def __init__( | |
14 | self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, | |
15 | style=wx.DEFAULT_DIALOG_STYLE | |
16 | ): | |
1e4a197e | 17 | |
95bfd958 | 18 | # Instead of calling wx.Dialog.__init__ we precreate the dialog |
8fa876ca | 19 | # so we can set an extra style that must be set before |
1e4a197e RD |
20 | # creation, and then we create the GUI dialog using the Create |
21 | # method. | |
8fa876ca RD |
22 | pre = wx.PreDialog() |
23 | pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP) | |
1e4a197e RD |
24 | pre.Create(parent, ID, title, pos, size, style) |
25 | ||
26 | # This next step is the most important, it turns this Python | |
27 | # object into the real wrapper of the dialog (instead of pre) | |
28 | # as far as the wxPython extension is concerned. | |
56b98bac | 29 | self.PostCreate(pre) |
1e4a197e RD |
30 | |
31 | # Now continue with the normal construction of the dialog | |
32 | # contents | |
8fa876ca | 33 | sizer = wx.BoxSizer(wx.VERTICAL) |
1e4a197e | 34 | |
95bfd958 | 35 | label = wx.StaticText(self, -1, "This is a wx.Dialog") |
1e4a197e | 36 | label.SetHelpText("This is the help text for the label") |
8fa876ca | 37 | sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5) |
1e4a197e | 38 | |
8fa876ca | 39 | box = wx.BoxSizer(wx.HORIZONTAL) |
1e4a197e | 40 | |
8fa876ca | 41 | label = wx.StaticText(self, -1, "Field #1:") |
1e4a197e | 42 | label.SetHelpText("This is the help text for the label") |
8fa876ca | 43 | box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5) |
031787de | 44 | |
8fa876ca | 45 | text = wx.TextCtrl(self, -1, "", size=(80,-1)) |
1e4a197e | 46 | text.SetHelpText("Here's some help text for field #1") |
8fa876ca | 47 | box.Add(text, 1, wx.ALIGN_CENTRE|wx.ALL, 5) |
031787de | 48 | |
ab5b7b61 | 49 | sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) |
031787de | 50 | |
8fa876ca | 51 | box = wx.BoxSizer(wx.HORIZONTAL) |
031787de | 52 | |
8fa876ca | 53 | label = wx.StaticText(self, -1, "Field #2:") |
1e4a197e | 54 | label.SetHelpText("This is the help text for the label") |
8fa876ca | 55 | box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5) |
031787de | 56 | |
8fa876ca | 57 | text = wx.TextCtrl(self, -1, "", size=(80,-1)) |
1e4a197e | 58 | text.SetHelpText("Here's some help text for field #2") |
8fa876ca | 59 | box.Add(text, 1, wx.ALIGN_CENTRE|wx.ALL, 5) |
031787de | 60 | |
ab5b7b61 | 61 | sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) |
031787de | 62 | |
8fa876ca RD |
63 | line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL) |
64 | sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5) | |
031787de | 65 | |
0f9da074 RD |
66 | btnsizer = wx.StdDialogButtonSizer() |
67 | ||
8fa876ca RD |
68 | if wx.Platform != "__WXMSW__": |
69 | btn = wx.ContextHelpButton(self) | |
0f9da074 RD |
70 | btnsizer.AddButton(btn) |
71 | ||
72 | btn = wx.Button(self, wx.ID_OK) | |
1e4a197e | 73 | btn.SetHelpText("The OK button completes the dialog") |
b3b61cda | 74 | btn.SetDefault() |
0f9da074 | 75 | btnsizer.AddButton(btn) |
031787de | 76 | |
0f9da074 | 77 | btn = wx.Button(self, wx.ID_CANCEL) |
8fa876ca | 78 | btn.SetHelpText("The Cancel button cnacels the dialog. (Cool, huh?)") |
0f9da074 | 79 | btnsizer.AddButton(btn) |
b3b61cda | 80 | btnsizer.Realize() |
031787de | 81 | |
0f9da074 | 82 | sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) |
031787de | 83 | |
1e4a197e RD |
84 | self.SetSizer(sizer) |
85 | self.SetAutoLayout(True) | |
86 | sizer.Fit(self) | |
031787de | 87 | |
1e4a197e RD |
88 | #--------------------------------------------------------------------------- |
89 | ||
34a544a6 RD |
90 | class TestPanel(wx.Panel): |
91 | def __init__(self, parent, log): | |
92 | self.log = log | |
93 | wx.Panel.__init__(self, parent, -1) | |
94 | ||
95 | b = wx.Button(self, -1, "Create and Show a custom Dialog", (50,50)) | |
96 | self.Bind(wx.EVT_BUTTON, self.OnButton, b) | |
97 | ||
98 | ||
99 | def OnButton(self, evt): | |
100 | dlg = TestDialog(self, -1, "This is a Dialog", size=(350, 200), | |
101 | #style = wxCAPTION | wxSYSTEM_MENU | wxTHICK_FRAME | |
102 | style = wx.DEFAULT_DIALOG_STYLE | |
103 | ) | |
104 | dlg.CenterOnScreen() | |
105 | ||
106 | # this does not return until the dialog is closed. | |
107 | val = dlg.ShowModal() | |
8fa876ca | 108 | |
34a544a6 RD |
109 | if val == wx.ID_OK: |
110 | self.log.WriteText("You pressed OK\n") | |
111 | else: | |
112 | self.log.WriteText("You pressed Cancel\n") | |
8fa876ca | 113 | |
34a544a6 RD |
114 | dlg.Destroy() |
115 | ||
cf694132 | 116 | |
34a544a6 RD |
117 | #--------------------------------------------------------------------------- |
118 | ||
119 | ||
120 | def runTest(frame, nb, log): | |
121 | win = TestPanel(nb, log) | |
122 | return win | |
cf694132 | 123 | |
cf694132 | 124 | |
8fa876ca | 125 | #--------------------------------------------------------------------------- |
cf694132 RD |
126 | |
127 | ||
8fa876ca RD |
128 | overview = """\ |
129 | wxPython offers quite a few general purpose dialogs for useful data input from | |
130 | the user; they are all based on the wx.Dialog class, which you can also subclass | |
131 | to create custom dialogs to suit your needs. | |
cf694132 | 132 | |
8fa876ca RD |
133 | The Dialog class, in addition to dialog-like behaviors, also supports the full |
134 | wxWindows layout featureset, which means that you can incorporate sizers or | |
135 | layout constraints as needed to achieve the look and feel desired. It even supports | |
136 | context-sensitive help, which is illustrated in this example. | |
cf694132 | 137 | |
8fa876ca RD |
138 | The example is very simple; in real world situations, a dialog that had input |
139 | fields such as this would no doubt be required to deliver those values back to | |
140 | the calling function. The Dialog class supports data retrieval in this manner. | |
141 | <b>However, the data must be retrieved prior to the dialog being destroyed.</b> | |
142 | The example shown here is <i>modal</i>; non-modal dialogs are possible as well. | |
cf694132 | 143 | |
8fa876ca | 144 | See the documentation for the <code>Dialog</code> class for more details. |
cf694132 | 145 | |
8fa876ca | 146 | """ |
cf694132 | 147 | |
1e4a197e RD |
148 | if __name__ == '__main__': |
149 | import sys,os | |
150 | import run | |
8eca4fef | 151 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
cf694132 | 152 |