Skip to content

Commit 4311a00

Browse files
mostly done -- except LAB!
1 parent 9b966f4 commit 4311a00

File tree

4 files changed

+159
-25
lines changed

4 files changed

+159
-25
lines changed

week-09/code/basic_app_7.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,21 @@ class ButtonPanel(wx.Panel):
4646
def __init__(self, *args, **kwargs):
4747
wx.Panel.__init__(self, *args, **kwargs)
4848

49-
## add two buttons:
49+
## add a button:
5050
theButton1 = wx.Button(self, label="Push Me")
5151
theButton1.Bind(wx.EVT_BUTTON, self.onButton)
5252

53-
## add two buttons:
53+
## add another button:
5454
theButton2 = wx.Button(self, label="Push Me Also")
5555
theButton2.Bind(wx.EVT_BUTTON, self.onButton)
5656

5757
## do the layout
58-
## (try uncommenting the other, and see what happens...)
5958
buttonSizer = wx.BoxSizer(wx.VERTICAL)
6059

61-
#buttonSizer.Add((1,1), 1) # stretchable space
6260
buttonSizer.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
6361
buttonSizer.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
64-
#buttonSizer.Add((1,1), 1) # stretchable space
6562

66-
## need another sizer to get the horizonal right:
63+
## need another sizer to get the horizonal placement right:
6764
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
6865
mainSizer.Add((1,1), 1) # stretchable space
6966
mainSizer.Add(buttonSizer, 0, wx.ALIGN_CENTER) # the sizer with the buttons in it

week-09/code/basic_app_8.py

+39-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
Example of the very basic, minimal framework for a wxPython application
55
6-
This adds a text box and a dialog box
6+
This adds a text box and reads the input from it.
77
"""
88

99

@@ -41,31 +41,52 @@ def file_close(self):
4141
print "Close a file: "
4242
print "I'd be closing a file now"
4343

44-
class ButtonPanel(wx.Panel):
44+
45+
class MainForm(wx.Panel):
4546
def __init__(self, *args, **kwargs):
4647
wx.Panel.__init__(self, *args, **kwargs)
4748

48-
## add two buttons:
49+
## add a button:
4950
theButton1 = wx.Button(self, label="Push Me")
5051
theButton1.Bind(wx.EVT_BUTTON, self.onButton)
5152

52-
## add two buttons:
53-
theButton2 = wx.Button(self, label="Push Me Also")
54-
theButton2.Bind(wx.EVT_BUTTON, self.onButton)
53+
## add a text control:
54+
self.inTextControl = wx.TextCtrl(self)
55+
56+
## add another button:
57+
theButton2 = wx.Button(self, label="GetData")
58+
theButton2.Bind(wx.EVT_BUTTON, self.onGetData)
59+
60+
## and another text control:
61+
self.outTextControl = wx.TextCtrl(self, style=wx.TE_READONLY)
62+
5563

5664
## do the layout
57-
## (try uncommenting the other, and see what happens...)
58-
S = wx.BoxSizer(wx.VERTICAL)
59-
#S = wx.BoxSizer(wx.HORIZONTAL)
65+
buttonSizer = wx.BoxSizer(wx.VERTICAL)
6066

61-
S.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
62-
S.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
63-
64-
self.SetSizerAndFit(S)
67+
buttonSizer.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
68+
buttonSizer.Add(self.inTextControl, 0, wx.GROW | wx.ALL, 4)
69+
buttonSizer.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
70+
buttonSizer.Add(self.outTextControl, 0, wx.GROW | wx.ALL, 4)
71+
72+
## need another sizer to get the horizonal placement right:
73+
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
74+
mainSizer.Add((1,1), 1) # stretchable space
75+
mainSizer.Add(buttonSizer, 0, wx.ALIGN_TOP) # the sizer with the buttons in it
76+
mainSizer.Add((1,1), 1) # stretchable space
77+
78+
self.SetSizer(mainSizer)
6579

6680
def onButton(self, evt=None):
6781
print "You pushed one of the buttons!"
6882

83+
def onGetData(self, evt=None):
84+
print "get data button pressed"
85+
contents = self.inTextControl.Value
86+
print "the contents are:", contents
87+
88+
self.outTextControl.Value = self.inTextControl.Value
89+
6990

7091
class TestFrame(wx.Frame):
7192
def __init__(self, app_logic, *args, **kwargs):
@@ -75,7 +96,7 @@ def __init__(self, app_logic, *args, **kwargs):
7596
self.app_logic = app_logic
7697

7798
# put the Panel on the frame
78-
self.buttonPanel = ButtonPanel(self)
99+
self.buttonPanel = MainForm(self)
79100

80101
# Build up the menu bar:
81102
menuBar = wx.MenuBar()
@@ -151,6 +172,10 @@ def OnInit(self):
151172
return True
152173

153174
if __name__ == "__main__":
175+
154176
app = TestApp(False)
177+
## set up the WIT -- to help debug sizers
178+
# import wx.lib.inspection
179+
# wx.lib.inspection.InspectionTool().Show()
155180
app.MainLoop()
156181

week-09/presentation-week09.pdf

16.7 KB
Binary file not shown.

week-09/presentation-week09.tex

+117-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ \section{Introduction}
7171

7272
\begin{frame}[fragile]{Python Options}
7373

74-
75-
7674
{\Large Multiple GUI frameworks available:}
7775

7876
\begin{itemize}
@@ -87,6 +85,7 @@ \section{Introduction}
8785

8886
\end{frame}
8987

88+
9089
\section{wxPython}
9190

9291
\begin{frame}[fragile]{wxPython}
@@ -203,6 +202,56 @@ \section{wxPython}
203202

204203
\end{frame}
205204

205+
%-------------------------------
206+
\begin{frame}[fragile]{Pythonic code:}
207+
208+
209+
\vfill
210+
{\Large Over the years, wxPython has grown a number of things to make it more ``pythonic'' -- hide some of that C++ legacy}
211+
212+
\vfill
213+
{\Large Properties:}
214+
215+
{\large The C++ classes are full of getters and setters:}
216+
\begin{verbatim}
217+
wxTextCtrl::GetValue
218+
wxTextCtrl::GetValue
219+
\end{verbatim}
220+
221+
{\large These methods have been translated into properties for Python}
222+
\begin{verbatim}
223+
MyTextCtrl.Value = some_string
224+
another_string = wxTextCtrl.Value
225+
\end{verbatim}
226+
227+
\vfill
228+
(The Get/Set versions are still there, but it's klunkier code)
229+
\end{frame}
230+
231+
%-------------------------------
232+
\begin{frame}[fragile]{Pythonic code:}
233+
234+
235+
\vfill
236+
{\Large Other Python options: some specific wx types can be accessed with standard python types:}
237+
238+
\vfill
239+
{\large \verb`wxPoint` --- \verb`(x,y)` ( tuple ) }
240+
241+
\vfill
242+
{\large \verb`wx.List` --- \verb`[1,2,3]` (python list) }
243+
244+
\vfill
245+
{\large \verb`wxSize` --- \verb`(w,h)` (tuple) }
246+
247+
\vfill
248+
{\large ....... }
249+
250+
251+
\vfill
252+
{\Large Using these makes your code cleaner and more pythonic}
253+
254+
\end{frame}
206255

207256
\section{Basic Structure}
208257

@@ -367,6 +416,7 @@ \section{Basic Structure}
367416

368417
\end{frame}
369418

419+
\section{controls}
370420

371421
%-------------------------------
372422
\begin{frame}[fragile]{Menus}
@@ -610,13 +660,13 @@ \section{Basic Structure}
610660
{\Large The alternative is ``Sizers''}
611661

612662
\vfill
613-
{\large \verb`wx.Sizer` is wx's system for automatically determining the size an location of controls}
663+
{\large \verb`wx.Sizer` is wx's system for automatically determining the size and location of controls}
614664

615665
\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:}
666+
{\large Instead of thinking in terms of what size and position a given control should be, you think in terms of how they relate to each other:}
617667

618668
\vfill
619-
{\large ``I want a column of buttons all the same size along the left edge of the Panel''}
669+
{\large \emph{``I want a column of buttons all the same size along the left edge of the Panel''} }
620670

621671
\vfill
622672
{\large Sizers capture that logic and compute the sizes for you}
@@ -735,7 +785,53 @@ \section{Basic Structure}
735785

736786
\end{frame}
737787

788+
789+
%-------------------------------
790+
\begin{frame}[fragile]{Accessing inputs}
791+
792+
{\Large Much of the point of a GUI is to collect data from the user.}
793+
794+
\vfill
795+
{\large So you need to be able to access what s/he has input}
796+
797+
\begin{verbatim}
798+
## add a text control:
799+
self.textControl = wx.TextCtrl(self)
800+
801+
def onGetData(self, evt=None):
802+
print "get data button pressed"
803+
contents = self.textControl.Value
804+
print "the contents are:", contents
805+
\end{verbatim}
806+
807+
{\large Most controls have a \verb`.Value` property}
808+
809+
\end{frame}
810+
738811
%-------------------------------
812+
\begin{frame}[fragile]{Setting Values}
813+
814+
{\Large You also want to display data...}
815+
816+
\vfill
817+
{\large So you need to be able to set the values, too:}
818+
819+
\begin{verbatim}
820+
## and another text control:
821+
self.outTextControl = wx.TextCtrl(self, style=wx.TE_READONLY)
822+
823+
def onGetData(self, evt=None):
824+
self.outTextControl.Value = self.inTextControl.Value
825+
826+
\end{verbatim}
827+
828+
{\large You can set the \verb`.Value` property too...}
829+
830+
\vfill
831+
example: \verb`code\basic_app8.py`
832+
833+
\end{frame}
834+
739835

740836
%-------------------------------
741837
\begin{frame}[fragile]{Long Running Tasks}
@@ -761,6 +857,8 @@ \section{Basic Structure}
761857

762858
\end{frame}
763859

860+
\section{Miscellaneous}
861+
764862
%-------------------------------
765863
\begin{frame}[fragile]{CallAfter}
766864

@@ -810,6 +908,20 @@ \section{Basic Structure}
810908
\vfill
811909
{\Large Example: Cameo Chemicals}
812910

911+
\vfill
912+
(PyCon 2009: Browser Interface, Local Server Application)
913+
\end{frame}
914+
915+
916+
%-------------------------------
917+
\begin{frame}[fragile]{LAB}
918+
919+
\vfill
920+
{\Large Make a very simple address book app:}
921+
\vfill
922+
923+
\verb`code\address_book\`
924+
813925
\end{frame}
814926

815927

0 commit comments

Comments
 (0)