-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic_app_4.py
More file actions
executable file
·199 lines (155 loc) · 6.91 KB
/
basic_app_4.py
File metadata and controls
executable file
·199 lines (155 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
"""
Example of the very basic, minimal framework for a wxPython application
This version adds a single button
"""
import wx
import os
#--------------------------------------------------------------
# This is how you pre-establish a file filter so that the dialog
# only shows the extension(s) you want it to.
wildcard = "Python source (*.py)|*.py|" \
"Compiled Python (*.pyc)|*.pyc|" \
"SPAM files (*.spam)|*.spam|" \
"Egg file (*.egg)|*.egg|" \
"All files (*.*)|*.*"
#--------------------------------------------------------------
class AppLogic(object):
"""
A class to hold the application Application Logic.
You generally don't want the real logic of the app mixed
in with the GUI
In a real app, this would be a substantial collection of
modules, classes, etc...
"""
def file_open(self, filename="default_name"):
"""This method opens a file"""
print "Open a file: "
print "I'd be opening file: %s now"%filename
def file_close(self):
"""This method closes a file"""
print "Close a file: "
print "I'd be closing a file now"
class TestFrame(wx.Frame):
def __init__(self, app_logic, *args, **kwargs):
kwargs.setdefault('title', "Simple test App")
wx.Frame.__init__(self, *args, **kwargs)
self.app_logic = app_logic
# Build up the menu bar:
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
newMenuItem = fileMenu.Append(wx.ID_ANY, "&Save As...", "Create a new file")
self.Bind(wx.EVT_MENU, self.onNew, newMenuItem )
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open an existing file" )
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
menuBar.Append(fileMenu, "&File")
helpMenu = wx.Menu()
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
menuBar.Append(helpMenu, "&Help")
self.SetMenuBar(menuBar)
## add just a single button:
self.theButton = wx.Button(self, label="Push Me")
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
self.theButton.Bind(wx.EVT_LEFT_DOWN, self.onButton)
def onButton(self, evt=None):
print "You pushed the button!"
evt.Skip()
def onClose(self, evt=None):
print "close menu selected"
self.file_close()
def onExit(self, evt=None):
print "Exit the program here"
print "The event passed to onExit is type ", type(evt),
self.Close()
def onNew ( self, evt=None ):
"""This method creates a new file"""
# Create the dialog. In this case the current directory is forced as the starting
# directory for the dialog, and no default file name is forced. This can easilly
# be changed in your program. This is an 'save' dialog.
#
# Unlike the 'open dialog' example found elsewhere, this example does NOT
# force the current working directory to change if the user chooses a different
# directory than the one initially set.
dlg = wx.FileDialog(self,
message="Save file as ...",
defaultDir=os.getcwd(),
defaultFile="",
wildcard=wildcard,
style=wx.SAVE )
# This sets the default filter that the user will initially see. Otherwise,
# the first filter in the list will be used by default.
dlg.SetFilterIndex(2)
# Show the dialog and retrieve the user response. If it is the OK response,
# process the data.
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
print "In onNew, the path is %s" % path
# Normally, at this point you would save your data using the file and path
# data that the user provided to you, but since we didn't actually start
# with any data to work with, that would be difficult.
#
# The code to do so would be similar to this, assuming 'data' contains
# the data you want to save:
#
# fp = file(path, 'w') # Create file anew
# fp.write(data)
# fp.close()
#
# You might want to add some error checking :-)
#
else :
print "The file dialog was canceled before anything was selected"
# Note that the current working dir didn't change. This is good since
# that's the way we set it up.
# Destroy the dialog. Don't do this until you are done with it!
# BAD things can happen otherwise!
dlg.Destroy()
def onOpen(self, evt=None):
"""This method opens an existing file"""
print "Open a file: "
# Create the dialog. In this case the current directory is forced as the starting
# directory for the dialog, and no default file name is forced. This can easilly
# be changed in your program. This is an 'open' dialog, and allows multitple
# file selections as well.
#
# Finally, if the directory is changed in the process of getting files, this
# dialog is set up to change the current working directory to the path chosen.
dlg = wx.FileDialog( self,
message="Choose a file",
defaultDir=os.getcwd(),
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.CHANGE_DIR
)
# Show the dialog and retrieve the user response. If it is the OK response,
# process the data.
if dlg.ShowModal() == wx.ID_OK:
# This returns a Python list of files that were selected.
path = dlg.GetPath()
print "I'd be opening file in onOpen ", path
self.app_logic.file_open( path )
else :
print "The file dialog was canceled before anything was selected"
# Destroy the dialog. Don't do this until you are done with it!
# BAD things can happen otherwise!
dlg.Destroy()
def file_close(self):
"""This method closes a file"""
print "Close a file: "
print "I'd be closing a file now"
class TestApp(wx.App):
def OnInit(self):
"""
App initilization goes here -- not much to do, in this case
"""
app_logic = AppLogic()
f = TestFrame(app_logic, parent=None)
f.Show()
return True
if __name__ == "__main__":
app = TestApp(False)
app.MainLoop()