]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Dialog.py
   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
) 
  10 #--------------------------------------------------------------------------- 
  12 class TestDialog(wx
.Dialog
): 
  14             self
, parent
, ID
, title
, size
=wx
.DefaultSize
, pos
=wx
.DefaultPosition
,  
  15             style
=wx
.DEFAULT_DIALOG_STYLE
, 
  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 
  24         pre
.SetExtraStyle(wx
.DIALOG_EX_CONTEXTHELP
) 
  25         pre
.Create(parent
, ID
, title
, pos
, size
, style
) 
  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. 
  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
) 
  37         # Now continue with the normal construction of the dialog 
  39         sizer 
= wx
.BoxSizer(wx
.VERTICAL
) 
  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) 
  45         box 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
  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) 
  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) 
  55         sizer
.Add(box
, 0, wx
.GROW|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5) 
  57         box 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
  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) 
  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) 
  67         sizer
.Add(box
, 0, wx
.GROW|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5) 
  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) 
  72         btnsizer 
= wx
.StdDialogButtonSizer() 
  74         if wx
.Platform 
!= "__WXMSW__": 
  75             btn 
= wx
.ContextHelpButton(self
) 
  76             btnsizer
.AddButton(btn
) 
  78         btn 
= wx
.Button(self
, wx
.ID_OK
) 
  79         btn
.SetHelpText("The OK button completes the dialog") 
  81         btnsizer
.AddButton(btn
) 
  83         btn 
= wx
.Button(self
, wx
.ID_CANCEL
) 
  84         btn
.SetHelpText("The Cancel button cnacels the dialog. (Cool, huh?)") 
  85         btnsizer
.AddButton(btn
) 
  88         sizer
.Add(btnsizer
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5) 
  93 #--------------------------------------------------------------------------- 
  95 class TestPanel(wx
.Panel
): 
  96     def __init__(self
, parent
, log
): 
  98         wx
.Panel
.__init
__(self
, parent
, -1) 
 100         b 
= wx
.Button(self
, -1, "Create and Show a custom Dialog", (50,50)) 
 101         self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
) 
 103         if 'wxMac' in wx
.PlatformInfo
: 
 104             self
.cb 
= wx
.CheckBox(self
, -1, "Set Metal appearance", (50,90)) 
 107     def OnButton(self
, evt
): 
 109         if 'wxMac' in wx
.PlatformInfo
: 
 110             useMetal 
= self
.cb
.IsChecked() 
 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, 
 119         # this does not return until the dialog is closed. 
 120         val 
= dlg
.ShowModal() 
 123             self
.log
.WriteText("You pressed OK\n") 
 125             self
.log
.WriteText("You pressed Cancel\n") 
 130 #--------------------------------------------------------------------------- 
 133 def runTest(frame
, nb
, log
): 
 134     win 
= TestPanel(nb
, log
) 
 138 #--------------------------------------------------------------------------- 
 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. 
 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. 
 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. 
 157 See the documentation for the <code>Dialog</code> class for more details. 
 161 if __name__ 
== '__main__': 
 164     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])