Skip to content

Commit 7bef7c8

Browse files
Chris BarkerChris Barker
Chris Barker
authored and
Chris Barker
committed
added version 4 and 5...
1 parent ae0c1f4 commit 7bef7c8

File tree

6 files changed

+320
-10
lines changed

6 files changed

+320
-10
lines changed

week-09/code/basic_app_1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def OnInit(self):
2323
return True
2424

2525
if __name__ == "__main__":
26-
app = TestApp()
26+
app = TestApp(False)
2727
app.MainLoop()
2828

week-09/code/basic_app_2.py

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ def OnInit(self):
8787
return True
8888

8989
if __name__ == "__main__":
90-
app = TestApp()
90+
app = TestApp(False)
9191
app.MainLoop()
9292

week-09/code/basic_app_4.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example of the very basic, minimal farmework for a wxPython application
5+
6+
This version adds a basic menu bar with a file menu
7+
"""
8+
9+
import wx
10+
11+
12+
class AppLogic(object):
13+
"""
14+
A class to hold the application Application Logic.
15+
16+
You generally don't want the real logic of the app mixed
17+
in with the GUI
18+
19+
In a real app, this would be a substantial collection of
20+
modules, classes, etc...
21+
"""
22+
def file_open(self, filename="default_name"):
23+
"""This method opens a file"""
24+
print "Open a file: "
25+
print "I'd be opening file: %s now"%filename
26+
27+
def file_close(self):
28+
"""This method closes a file"""
29+
print "Close a file: "
30+
print "I'd be closing a file now"
31+
32+
33+
class TestFrame(wx.Frame):
34+
def __init__(self, app_logic, *args, **kwargs):
35+
kwargs.setdefault('title', "Simple test App")
36+
wx.Frame.__init__(self, *args, **kwargs)
37+
38+
self.app_logic = app_logic
39+
40+
# Build up the menu bar:
41+
menuBar = wx.MenuBar()
42+
43+
fileMenu = wx.Menu()
44+
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" )
45+
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
46+
47+
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
48+
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
49+
50+
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
51+
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
52+
menuBar.Append(fileMenu, "&File")
53+
54+
helpMenu = wx.Menu()
55+
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
56+
menuBar.Append(helpMenu, "&Help")
57+
58+
self.SetMenuBar(menuBar)
59+
60+
## add just a single button:
61+
self.theButton = wx.Button(self, label="Push Me")
62+
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
63+
64+
def onButton(self, evt=None):
65+
print "You pushed the button!"
66+
67+
def onOpen(self, evt=None):
68+
print "open menu selected"
69+
self.app_logic.file_open()
70+
71+
def onClose(self, evt=None):
72+
print "close menu selected"
73+
self.app_logic.file_close()
74+
75+
def onExit(self, evt=None):
76+
print "Exit the program here"
77+
print "The event passed to onExit is type ", type(evt),
78+
self.Close()
79+
80+
81+
class TestApp(wx.App):
82+
def OnInit(self):
83+
"""
84+
App initilization goes here -- not much to do, in this case
85+
"""
86+
app_logic = AppLogic()
87+
f = TestFrame(app_logic, parent=None)
88+
f.Show()
89+
90+
return True
91+
92+
if __name__ == "__main__":
93+
app = TestApp(False)
94+
app.MainLoop()
95+

week-09/code/basic_app_5.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example of the very basic, minimal farmework for a wxPython application
5+
6+
This version adds a basic menu bar with a file menu
7+
"""
8+
9+
import wx
10+
11+
12+
class AppLogic(object):
13+
"""
14+
A class to hold the application Application Logic.
15+
16+
You generally don't want the real logic of the app mixed
17+
in with the GUI
18+
19+
In a real app, this would be a substantial collection of
20+
modules, classes, etc...
21+
"""
22+
def file_open(self, filename="default_name"):
23+
"""This method opens a file"""
24+
print "Open a file: "
25+
print "I'd be opening file: %s now"%filename
26+
27+
def file_close(self):
28+
"""This method closes a file"""
29+
print "Close a file: "
30+
print "I'd be closing a file now"
31+
32+
class ButtonPanel(wx.Panel):
33+
def __init__(self, *args, **kwargs):
34+
wx.Panel.__init__(self, *args, **kwargs)
35+
36+
## add just a single button:
37+
self.theButton = wx.Button(self, label="Push Me")
38+
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
39+
40+
def onButton(self, evt=None):
41+
print "You pushed the button!"
42+
43+
class TestFrame(wx.Frame):
44+
def __init__(self, app_logic, *args, **kwargs):
45+
kwargs.setdefault('title', "Simple test App")
46+
wx.Frame.__init__(self, *args, **kwargs)
47+
48+
self.app_logic = app_logic
49+
50+
# put the Panel on the frame
51+
self.buttonPanel = ButtonPanel(self)
52+
53+
# Build up the menu bar:
54+
menuBar = wx.MenuBar()
55+
56+
fileMenu = wx.Menu()
57+
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" )
58+
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
59+
60+
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
61+
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
62+
63+
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
64+
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
65+
menuBar.Append(fileMenu, "&File")
66+
67+
helpMenu = wx.Menu()
68+
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
69+
menuBar.Append(helpMenu, "&Help")
70+
71+
self.SetMenuBar(menuBar)
72+
73+
74+
def onOpen(self, evt=None):
75+
print "open menu selected"
76+
self.app_logic.file_open()
77+
78+
def onClose(self, evt=None):
79+
print "close menu selected"
80+
self.app_logic.file_close()
81+
82+
def onExit(self, evt=None):
83+
print "Exit the program here"
84+
print "The event passed to onExit is type ", type(evt),
85+
self.Close()
86+
87+
88+
class TestApp(wx.App):
89+
def OnInit(self):
90+
"""
91+
App initilization goes here -- not much to do, in this case
92+
"""
93+
app_logic = AppLogic()
94+
f = TestFrame(app_logic, parent=None)
95+
f.Show()
96+
97+
return True
98+
99+
if __name__ == "__main__":
100+
app = TestApp(False)
101+
app.MainLoop()
102+

week-09/presentation-week09.pdf

15.3 KB
Binary file not shown.

week-09/presentation-week09.tex

+121-8
Original file line numberDiff line numberDiff line change
@@ -421,34 +421,147 @@ \section{Basic Structure}
421421

422422

423423
%-------------------------------
424-
\begin{frame}[fragile]{Title}
424+
\begin{frame}[fragile]{Common Dialogs}
425425

426426
\vfill
427-
{\Large ``Stable'' version: 2.8.12.1}\\
427+
{\Large wxPython provides a number of common Dialogs. These wrap the native ones where possible for a native look and feel.}
428+
429+
\begin{itemize}
430+
\item \verb`wx.MessageDialog`
431+
\item \verb`wx.ColourDialog`
432+
\item \verb`wx.FileDialog`
433+
\item \verb`wx.PageSetupDialog`
434+
\item \verb`wx.FontDialog`
435+
\item \verb`wx.DirDialog`
436+
\item \verb`wx.SingleChoiceDialog`
437+
\item \verb`wx.TextEntryDialog`
438+
\item ...
439+
\end{itemize}
440+
441+
\vfill
442+
{\Large These do pretty much what you'd expect...}
428443

429444
\end{frame}
430445

431446
%-------------------------------
432-
\begin{frame}[fragile]{Title}
447+
\begin{frame}[fragile]{wx.FileDialog}
433448

434449
\vfill
435-
{\Large ``Stable'' version: 2.8.12.1}\\
450+
{\Large Example use of a common dialog: \verb`wx.FileDialog`}\\
451+
452+
\begin{verbatim}
453+
some code here
454+
\end{verbatim}
436455

456+
\vfill
457+
example: \verb`code/basic_app_3.py`
437458
\end{frame}
438459

439460
%-------------------------------
440-
\begin{frame}[fragile]{Title}
461+
\begin{frame}[fragile]{Basic Widgets}
441462

442463
\vfill
443-
{\Large ``Stable'' version: 2.8.12.1}\\
464+
{\Large All the basic widgets (controls) you'd expect are there:}
465+
466+
\begin{itemize}
467+
\item Buttons
468+
\item TextCtrl (Text Control)
469+
\item Check Boxes
470+
\item List Box
471+
\item Combo Box
472+
\item Slider
473+
\item Spin Control
474+
\item ....
475+
\end{itemize}
476+
477+
\vfill
478+
{\Large Way too many to list here!}
479+
480+
\vfill
481+
{\Large See the docs and the Demo to find the one you need}
444482

445483
\end{frame}
446484

447485
%-------------------------------
448-
\begin{frame}[fragile]{Title}
486+
\begin{frame}[fragile]{Using a Control}
487+
488+
{\Large A Button about as simple as it gets}
449489

450490
\vfill
451-
{\Large ``Stable'' version: 2.8.12.1}\\
491+
{\verb`__init__(parent, id=-1, label="", pos=wx.DefaultPosition, ...)` }
492+
493+
\vfill
494+
{\Large Mostly the same as wx.Window, and other controls....}
495+
496+
\begin{verbatim}
497+
## add just a single button:
498+
self.theButton = wx.Button(self, label="Push Me")
499+
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
500+
501+
## and give it an event handler
502+
def onButton(self, evt=None):
503+
print "You pushed the button!"
504+
\end{verbatim}
505+
506+
\vfill
507+
code: \verb`code\basic_app_4.py`
508+
\end{frame}
509+
510+
%-------------------------------
511+
\begin{frame}[fragile]{wx.Panel}
512+
513+
{\Large A \verb`wx.Panel` is a \verb`wx.Window` that you can put other controls on}
514+
515+
\vfill
516+
{\Large It supplies nifty things like tab traversal, etc.}
517+
518+
\vfill
519+
{\Large You \emph{can} put controls right on a \verb`wx.Frame` (we just did it), but a wx.Panel provided extra features, the ``normal'' look, and helps you organize and re-use your code}
520+
521+
\vfill
522+
{\Large Mostly the same as wx.Window, and other controls....}
523+
524+
\end{frame}
525+
526+
%-------------------------------
527+
\begin{frame}[fragile]{wx.Panel}
528+
529+
\begin{verbatim}
530+
class ButtonPanel(wx.Panel):
531+
def __init__(self, *args, **kwargs):
532+
wx.Panel.__init__(self, *args, **kwargs)
533+
534+
## add just a single button:
535+
self.theButton = wx.Button(self, label="Push Me")
536+
self.theButton.Bind(wx.EVT_BUTTON, self.onButton)
537+
538+
def onButton(self, evt=None):
539+
print "You pushed the button!"
540+
\end{verbatim}
541+
And use it in the Frame:
542+
\begin{verbatim}
543+
# put the Panel on the frame
544+
self.buttonPanel = ButtonPanel(self)
545+
\end{verbatim}
546+
547+
\vfill
548+
code: \verb`code\basic_app_5.py`
549+
\end{frame}
550+
551+
%-------------------------------
552+
\begin{frame}[fragile]{Control Layout}
553+
554+
{\Large With more than one control, you need to figure out how to place them
555+
and how big to make them}
556+
557+
\vfill
558+
{\Large You may have noticed that \verb`wx.Window` takes \verb`pos` and \verb`size` parameters}
559+
560+
\vfill
561+
{\Large You may have also noticed that I didn't use them.}
562+
563+
\vfill
564+
{\Large Why not?}
452565

453566
\end{frame}
454567

0 commit comments

Comments
 (0)