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