-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic_app_8.py
More file actions
executable file
·190 lines (143 loc) · 6.22 KB
/
basic_app_8.py
File metadata and controls
executable file
·190 lines (143 loc) · 6.22 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
#!/usr/bin/env python
"""
Example of the very basic, minimal framework for a wxPython application
This adds a text box and reads the input from it, and writes it
to another text box
"""
import wx
#---------------------------------------------------------------------------
# This is how you pre-establish a file filter so that file dialogs
# only show 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 MainForm(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
## add a button:
theButton1 = wx.Button(self, label="Push Me")
theButton1.Bind(wx.EVT_BUTTON, self.onButton)
## add a static text lable:
label1 = wx.StaticText(self, label="Input Box:")
## add a text control:
self.inTextControl = wx.TextCtrl(self)
## add another button:
theButton2 = wx.Button(self, label="GetData")
theButton2.Bind(wx.EVT_BUTTON, self.onGetData)
## add a static text lable:
label2 = wx.StaticText(self, label="Output Box:")
## and another text control:
self.outTextControl = wx.TextCtrl(self, style=wx.TE_READONLY)
## do the layout
buttonSizer = wx.BoxSizer(wx.VERTICAL)
buttonSizer.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
buttonSizer.Add(label1, 0, wx.ALIGN_LEFT | wx.TOP, 4)
buttonSizer.Add(self.inTextControl, 0, wx.GROW | wx.ALL, 4)
buttonSizer.Add((150, 10))
buttonSizer.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
buttonSizer.Add(label2, 0, wx.ALIGN_LEFT | wx.TOP, 4)
buttonSizer.Add(self.outTextControl, 0, wx.GROW | wx.ALL, 4)
## need another sizer to get the horizonal placement right:
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add((1,1), 1) # stretchable space
mainSizer.Add(buttonSizer, 0, wx.ALIGN_TOP) # the sizer with the buttons in it
mainSizer.Add((1,1), 1) # stretchable space
self.SetSizer(mainSizer)
def onButton(self, evt=None):
print "You pushed one of the buttons!"
def onGetData(self, evt=None):
print "get data button pressed"
contents = self.inTextControl.Value
print "the contents are:", contents
self.outTextControl.Value = self.inTextControl.Value
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
# put the Panel on the frame
self.buttonPanel = MainForm(self)
# Build up the menu bar:
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a 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)
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 easily
# be changed in your program. This is an 'open' dialog, and allows multiple
# 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 onClose(self, evt=None):
print "close menu selected"
self.app_logic.file_close()
def onExit(self, evt=None):
print "Exit the program here"
print "The event passed to onExit is type ", type(evt),
self.Close()
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)
## set up the WIT -- to help debug sizers
# import wx.lib.inspection
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()