Skip to content

Commit 8089e6c

Browse files
Chris BarkerChris Barker
Chris Barker
authored and
Chris Barker
committed
added sizer example
1 parent 4a34a85 commit 8089e6c

File tree

5 files changed

+229
-6
lines changed

5 files changed

+229
-6
lines changed

week-09/code/basic_app_3.py

100644100755
+1-4
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ def onOpen(self, evt=None):
167167
# This returns a Python list of files that were selected.
168168
path = dlg.GetPath()
169169

170-
171-
172-
173170
# Destroy the dialog. Don't do this until you are done with it!
174171
# BAD things can happen otherwise!
175172
dlg.Destroy()
@@ -196,6 +193,6 @@ def OnInit(self):
196193
return True
197194

198195
if __name__ == "__main__":
199-
app = TestApp()
196+
app = TestApp(False)
200197
app.MainLoop()
201198

week-09/code/basic_app_5.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22

33
"""
4-
Example of the very basic, minimal farmework for a wxPython application
4+
Example of the very basic, minimal framework for a wxPython application
55
6-
This version adds a basic menu bar with a file menu
6+
This version adds a panel for the controls.
77
"""
88

99
import wx

week-09/code/basic_app_6b.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Example of the very basic, minimal framework for a wxPython application
5+
6+
This version adds a BoxSizer for laying out buttons on the panel
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 two buttons:
37+
theButton1 = wx.Button(self, label="Push Me")
38+
theButton1.Bind(wx.EVT_BUTTON, self.onButton)
39+
40+
## add two buttons:
41+
theButton2 = wx.Button(self, label="Push Me Also")
42+
theButton2.Bind(wx.EVT_BUTTON, self.onButton)
43+
44+
## do the layout
45+
## (try uncommenting the other, and see what happens...)
46+
S = wx.BoxSizer(wx.VERTICAL)
47+
#S = wx.BoxSizer(wx.HORIZONTAL)
48+
49+
S.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
50+
S.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
51+
52+
self.SetSizerAndFit(S)
53+
54+
def onButton(self, evt=None):
55+
print "You pushed one of the buttons!"
56+
57+
58+
class TestFrame(wx.Frame):
59+
def __init__(self, app_logic, *args, **kwargs):
60+
kwargs.setdefault('title', "Simple test App")
61+
wx.Frame.__init__(self, *args, **kwargs)
62+
63+
self.app_logic = app_logic
64+
65+
# put the Panel on the frame
66+
self.buttonPanel = ButtonPanel(self)
67+
68+
# Build up the menu bar:
69+
menuBar = wx.MenuBar()
70+
71+
fileMenu = wx.Menu()
72+
openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" )
73+
self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
74+
75+
closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" )
76+
self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
77+
78+
exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application")
79+
self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
80+
menuBar.Append(fileMenu, "&File")
81+
82+
helpMenu = wx.Menu()
83+
helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help")
84+
menuBar.Append(helpMenu, "&Help")
85+
86+
self.SetMenuBar(menuBar)
87+
88+
89+
def onOpen(self, evt=None):
90+
print "open menu selected"
91+
self.app_logic.file_open()
92+
93+
def onClose(self, evt=None):
94+
print "close menu selected"
95+
self.app_logic.file_close()
96+
97+
def onExit(self, evt=None):
98+
print "Exit the program here"
99+
print "The event passed to onExit is type ", type(evt),
100+
self.Close()
101+
102+
103+
class TestApp(wx.App):
104+
def OnInit(self):
105+
"""
106+
App initilization goes here -- not much to do, in this case
107+
"""
108+
app_logic = AppLogic()
109+
f = TestFrame(app_logic, parent=None)
110+
f.Show()
111+
112+
return True
113+
114+
if __name__ == "__main__":
115+
app = TestApp(False)
116+
app.MainLoop()
117+

week-09/presentation-week09.pdf

11.4 KB
Binary file not shown.

week-09/presentation-week09.tex

+109
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,115 @@ \section{Basic Structure}
565565

566566
\end{frame}
567567

568+
%-------------------------------
569+
\begin{frame}[fragile]{Absolute Positioning}
570+
571+
{\LARGE Absolute positioning:}
572+
573+
\vfill
574+
{\Large Specifying the size and location of controls with pixel coordinates.}
575+
576+
\vfill
577+
{\Large This is a serious pain to do!}
578+
579+
\vfill
580+
{\Large Though it can be made a lot easier with GUI-building tools...}
581+
582+
\vfill
583+
{\Large So why not?}
584+
585+
\end{frame}
586+
587+
%-------------------------------
588+
\begin{frame}[fragile]{Absolute Positioning}
589+
590+
{\Large When you add or remove a control, the layout changes:}\\
591+
{\large -- recalculate all positions and sizes}
592+
593+
\vfill
594+
{\Large When you change the text on a control the layout changes:}\\
595+
{\large -- recalculate all positions and sizes}
596+
597+
\vfill
598+
{\Large When you try it on another platform the layout changes:}\\
599+
{\large -- recalculate all positions and sizes}
600+
601+
\vfill
602+
{\Large When the user changes default font size, the layout changes:}\\
603+
{\large -- recalculate all positions and sizes}
604+
605+
\end{frame}
606+
607+
%-------------------------------
608+
\begin{frame}[fragile]{Sizers:}
609+
610+
{\Large The alternative is ``Sizers''}
611+
612+
\vfill
613+
{\large \verb`wx.Sizer` is wx's system for automatically determining the size an location of controls}
614+
615+
\vfill
616+
{\large Instead of thinking in terms of what size an position a given control should be, you think in terms of how they relate to each other:}
617+
618+
\vfill
619+
{\large ``I want a column of buttons all the same size along the left edge of the Panel''}
620+
621+
\vfill
622+
{\large Sizers capture that logic and compute the sizes for you}
623+
624+
\vfill
625+
{\large They will re-size things for you when anything changes -- adding, removing, changing labels, re-sing the Window, etc...}
626+
627+
\end{frame}
628+
629+
630+
%-------------------------------
631+
\begin{frame}[fragile]{Sizers:}
632+
633+
{\Large Sizers take a while to wrap your brain around...}
634+
635+
\vfill
636+
{\large but it's worth the learning curve.}
637+
638+
639+
\vfill
640+
{\large nice discussion here:\\
641+
\url{http://wiki.wxpython.org/UsingSizers} }
642+
643+
\vfill
644+
{I have the graphic posted on the wall by my desk...}
645+
646+
\end{frame}
647+
648+
%-------------------------------
649+
\begin{frame}[fragile]{Sizer Example}
650+
651+
{\Large The Basic \verb`BoxSizer`}
652+
653+
\vfill
654+
{\large Lays out a row or column of controls...}
655+
656+
\vfill
657+
{\Large Adding an item to a sizer:}
658+
{ \verb`Sizer.Add( window, proportion, flag, border )` }
659+
660+
\vfill
661+
\begin{verbatim}
662+
## do the layout
663+
S = wx.BoxSizer(wx.VERTICAL)
664+
665+
S.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
666+
S.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
667+
668+
self.SetSizerAndFit(S)
669+
\end{verbatim}
670+
671+
\vfill
672+
code: \verb`code\basic_app_6.py`
673+
\end{frame}
674+
675+
676+
568677
%-------------------------------
569678
\begin{frame}[fragile]{Long Running Tasks}
570679

0 commit comments

Comments
 (0)