1
1
#!/usr/bin/env python
2
2
3
3
"""
4
- Example of the very basic, minimal farmework for a wxPython application
4
+ Example of the very basic, minimal framework for a wxPython application
5
5
6
- This version adds a basic menu bar with a file menu
6
+ This version adds a single button
7
7
"""
8
8
9
9
import wx
10
10
import os
11
11
12
- #---------------------------------------------------------------------------
12
+ #--------------------------------------------------------------
13
13
14
14
# This is how you pre-establish a file filter so that the dialog
15
15
# only shows the extension(s) you want it to.
19
19
"Egg file (*.egg)|*.egg|" \
20
20
"All files (*.*)|*.*"
21
21
22
- #---------------------------------------------------------------------------
23
-
22
+ #--------------------------------------------------------------
24
23
25
24
class AppLogic (object ):
26
25
"""
@@ -44,9 +43,6 @@ def file_close(self):
44
43
print "I'd be closing a file now"
45
44
46
45
47
-
48
-
49
-
50
46
class TestFrame (wx .Frame ):
51
47
def __init__ (self , app_logic , * args , ** kwargs ):
52
48
kwargs .setdefault ('title' , "Simple test App" )
@@ -56,55 +52,44 @@ def __init__(self, app_logic, *args, **kwargs):
56
52
57
53
# Build up the menu bar:
58
54
menuBar = wx .MenuBar ()
59
-
60
- # Creating a file menu
55
+
61
56
fileMenu = wx .Menu ()
62
-
63
- # Creating an Open option on the file menu.
64
- openMenuItem = fileMenu .Append (wx .ID_ANY , "&Open" , "Open a file" )
57
+
58
+ newMenuItem = fileMenu .Append (wx .ID_ANY , "&Save As..." , "Create a new file" )
59
+ self .Bind (wx .EVT_MENU , self .onNew , newMenuItem )
60
+
61
+ openMenuItem = fileMenu .Append (wx .ID_ANY , "&Open" , "Open an existing file" )
65
62
self .Bind (wx .EVT_MENU , self .onOpen , openMenuItem )
66
63
67
- # Creating a Close option on the file menu
68
64
closeMenuItem = fileMenu .Append (wx .ID_ANY , "&Close" , "Close a file" )
69
65
self .Bind (wx .EVT_MENU , self .onClose , closeMenuItem )
70
66
71
- # Creating an Exit option on the file menu
72
67
exitMenuItem = fileMenu .Append (wx .ID_EXIT , "Exit" , "Exit the application" )
73
68
self .Bind (wx .EVT_MENU , self .onExit , exitMenuItem )
74
-
75
- # The file menu goes on the menuBar
76
69
menuBar .Append (fileMenu , "&File" )
77
-
78
- # Create a help menu
70
+
79
71
helpMenu = wx .Menu ()
80
-
81
- # The help menu has a single option, help.
82
72
helpMenuItem = helpMenu .Append (wx .ID_HELP , "Help" , "Get help" )
83
-
84
- # The help menu goes on the menuBar
85
73
menuBar .Append (helpMenu , "&Help" )
86
74
87
- # The menuBar goes on the frame.
88
75
self .SetMenuBar (menuBar )
89
-
76
+
90
77
## add just a single button:
91
78
self .theButton = wx .Button (self , label = "Push Me" )
92
79
self .theButton .Bind (wx .EVT_BUTTON , self .onButton )
93
80
94
81
def onButton (self , evt = None ):
95
82
print "You pushed the button!"
96
83
84
+ def onClose (self , evt = None ):
85
+ print "close menu selected"
86
+ self .file_close ()
97
87
98
88
def onExit (self , evt = None ):
99
89
print "Exit the program here"
100
90
print "The event passed to onExit is type " , type (evt ),
101
91
self .Close ()
102
92
103
- def onClose (self , evt = None ):
104
- print "close menu selected"
105
- self .app_logic .file_close ()
106
-
107
-
108
93
def onNew ( self , evt = None ):
109
94
"""This method creates a new file"""
110
95
@@ -115,9 +100,12 @@ def onNew ( self, evt=None ):
115
100
# Unlike the 'open dialog' example found elsewhere, this example does NOT
116
101
# force the current working directory to change if the user chooses a different
117
102
# directory than the one initially set.
118
- dlg = wx .FileDialog (
119
- self , message = "Save file as ..." , defaultDir = os .getcwd (),
120
- defaultFile = "" , wildcard = wildcard , style = wx .SAVE )
103
+ dlg = wx .FileDialog (self ,
104
+ message = "Save file as ..." ,
105
+ defaultDir = os .getcwd (),
106
+ defaultFile = "" ,
107
+ wildcard = wildcard ,
108
+ style = wx .SAVE )
121
109
122
110
# This sets the default filter that the user will initially see. Otherwise,
123
111
# the first filter in the list will be used by default.
@@ -150,9 +138,7 @@ def onNew ( self, evt=None ):
150
138
# Destroy the dialog. Don't do this until you are done with it!
151
139
# BAD things can happen otherwise!
152
140
dlg .Destroy ()
153
-
154
141
155
-
156
142
157
143
def onOpen (self , evt = None ):
158
144
"""This method opens an existing file"""
@@ -164,13 +150,13 @@ def onOpen(self, evt=None):
164
150
#
165
151
# Finally, if the directory is changed in the process of getting files, this
166
152
# dialog is set up to change the current working directory to the path chosen.
167
- dlg = wx .FileDialog (
168
- self , message = "Choose a file" ,
169
- defaultDir = os .getcwd (),
170
- defaultFile = "" ,
171
- wildcard = wildcard ,
172
- style = wx .OPEN | wx .CHANGE_DIR
173
- )
153
+ dlg = wx .FileDialog ( self ,
154
+ message = "Choose a file" ,
155
+ defaultDir = os .getcwd (),
156
+ defaultFile = "" ,
157
+ wildcard = wildcard ,
158
+ style = wx .OPEN | wx .CHANGE_DIR
159
+ )
174
160
175
161
# Show the dialog and retrieve the user response. If it is the OK response,
176
162
# process the data.
@@ -181,16 +167,17 @@ def onOpen(self, evt=None):
181
167
self .app_logic .file_open ( path )
182
168
else :
183
169
print "The file dialog was canceled before anything was selected"
184
-
185
-
186
-
187
170
188
171
# Destroy the dialog. Don't do this until you are done with it!
189
172
# BAD things can happen otherwise!
190
173
dlg .Destroy ()
191
174
192
175
193
-
176
+ def file_close (self ):
177
+ """This method closes a file"""
178
+ print "Close a file: "
179
+ print "I'd be closing a file now"
180
+
194
181
195
182
class TestApp (wx .App ):
196
183
def OnInit (self ):
0 commit comments