~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/demo/Dialog.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

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