Skip to content

Commit 3b21cfb

Browse files
committed
Very basic outline of a text box and a dialog box
1 parent 3950355 commit 3b21cfb

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

week-09/code/basic_app_6.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example of the very basic, minimal farmework for a wxPython application
5+
6+
This shows how to do a text box and a dialog box
7+
"""
8+
9+
import wx
10+
import os
11+
12+
#---------------------------------------------------------------------------
13+
14+
# This is how you pre-establish a file filter so that the dialog
15+
# only shows the extension(s) you want it to.
16+
wildcard = "Python source (*.py)|*.py|" \
17+
"Compiled Python (*.pyc)|*.pyc|" \
18+
"SPAM files (*.spam)|*.spam|" \
19+
"Egg file (*.egg)|*.egg|" \
20+
"All files (*.*)|*.*"
21+
22+
#---------------------------------------------------------------------------
23+
24+
25+
class AppLogic(object):
26+
"""
27+
A class to hold the application Application Logic.
28+
29+
You generally don't want the real logic of the app mixed
30+
in with the GUI
31+
32+
In a real app, this would be a substantial collection of
33+
modules, classes, etc...
34+
"""
35+
def file_open(self, filename="default_name"):
36+
"""This method opens a file"""
37+
print "Open a file: "
38+
print "I'd be opening file: %s now"%filename
39+
40+
def file_close(self):
41+
"""This method closes a file"""
42+
print "Close a file: "
43+
print "I'd be closing a file now"
44+
45+
class ButtonPanel(wx.Panel):
46+
def __init__(self, *args, **kwargs):
47+
wx.Panel.__init__(self, *args, **kwargs)
48+
49+
## add just a single button:
50+
self.theButton = wx.Button(self, label="Push Me")
51+
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
52+
53+
def onButton(self, evt=None):
54+
print "You pushed the button!"
55+
56+
class TestFrame(wx.Frame):
57+
def __init__(self, app_logic, *args, **kwargs):
58+
kwargs.setdefault('title', "Simple test App")
59+
wx.Frame.__init__(self, *args, **kwargs)
60+
61+
self.app_logic = app_logic
62+
63+
# put the Panel on the frame
64+
self.buttonPanel = ButtonPanel(self)
65+
66+
# Build up the menu bar:
67+
menuBar = wx.MenuBar()
68+
69+
fileMenu = wx.Menu()
70+
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" )
71+
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
72+
73+
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
74+
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
75+
76+
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
77+
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
78+
menuBar.Append(fileMenu, "&File")
79+
80+
helpMenu = wx.Menu()
81+
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
82+
menuBar.Append(helpMenu, "&Help")
83+
84+
self.SetMenuBar(menuBar)
85+
86+
87+
def onOpen(self, evt=None):
88+
"""This method opens an existing file"""
89+
print "Open a file: "
90+
# Create the dialog. In this case the current directory is forced as the starting
91+
# directory for the dialog, and no default file name is forced. This can easilly
92+
# be changed in your program. This is an 'open' dialog, and allows multitple
93+
# file selections as well.
94+
#
95+
# Finally, if the directory is changed in the process of getting files, this
96+
# dialog is set up to change the current working directory to the path chosen.
97+
dlg = wx.FileDialog(
98+
self, message="Choose a file",
99+
defaultDir=os.getcwd(),
100+
defaultFile="",
101+
wildcard=wildcard,
102+
style=wx.OPEN | wx.CHANGE_DIR
103+
)
104+
105+
# Show the dialog and retrieve the user response. If it is the OK response,
106+
# process the data.
107+
if dlg.ShowModal() == wx.ID_OK:
108+
# This returns a Python list of files that were selected.
109+
path = dlg.GetPath()
110+
print "I'd be opening file in onOpen ", path
111+
self.app_logic.file_open( path )
112+
else :
113+
print "The file dialog was canceled before anything was selected"
114+
115+
116+
117+
118+
# Destroy the dialog. Don't do this until you are done with it!
119+
# BAD things can happen otherwise!
120+
dlg.Destroy()
121+
122+
123+
124+
def onClose(self, evt=None):
125+
print "close menu selected"
126+
self.app_logic.file_close()
127+
128+
def onExit(self, evt=None):
129+
print "Exit the program here"
130+
print "The event passed to onExit is type ", type(evt),
131+
self.Close()
132+
133+
134+
class TestApp(wx.App):
135+
def OnInit(self):
136+
"""
137+
App initilization goes here -- not much to do, in this case
138+
"""
139+
app_logic = AppLogic()
140+
f = TestFrame(app_logic, parent=None)
141+
f.Show()
142+
143+
return True
144+
145+
if __name__ == "__main__":
146+
app = TestApp(False)
147+
app.MainLoop()
148+

0 commit comments

Comments
 (0)