Skip to content

Commit 2ca1c29

Browse files
Chris BarkerChris Barker
Chris Barker
authored and
Chris Barker
committed
re-shuffled app versions 6 and 7...
1 parent 8d4f3b6 commit 2ca1c29

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

week-09/code/basic_app_6.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
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 shows how to do a text box and a dialog box
6+
This version adds a BoxSizer for laying out buttons on the panel
77
"""
88

99
import wx
10-
import os
1110

1211
#---------------------------------------------------------------------------
1312

14-
# This is how you pre-establish a file filter so that the dialog
15-
# only shows the extension(s) you want it to.
13+
# This is how you pre-establish a file filter so that file dialogs
14+
# only show the extension(s) you want it to.
1615
wildcard = "Python source (*.py)|*.py|" \
1716
"Compiled Python (*.pyc)|*.pyc|" \
1817
"SPAM files (*.spam)|*.spam|" \
@@ -21,7 +20,6 @@
2120

2221
#---------------------------------------------------------------------------
2322

24-
2523
class AppLogic(object):
2624
"""
2725
A class to hold the application Application Logic.
@@ -46,12 +44,27 @@ class ButtonPanel(wx.Panel):
4644
def __init__(self, *args, **kwargs):
4745
wx.Panel.__init__(self, *args, **kwargs)
4846

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-
47+
## add two buttons:
48+
theButton1 = wx.Button(self, label="Push Me")
49+
theButton1.Bind(wx.EVT_BUTTON, self.onButton)
50+
51+
## add two buttons:
52+
theButton2 = wx.Button(self, label="Push Me Also")
53+
theButton2.Bind(wx.EVT_BUTTON, self.onButton)
54+
55+
## do the layout
56+
## (try uncommenting the other, and see what happens...)
57+
S = wx.BoxSizer(wx.VERTICAL)
58+
#S = wx.BoxSizer(wx.HORIZONTAL)
59+
60+
S.Add(theButton1, 0, wx.GROW | wx.ALL, 4)
61+
S.Add(theButton2, 0, wx.GROW | wx.ALL, 4)
62+
63+
self.SetSizerAndFit(S)
64+
5365
def onButton(self, evt=None):
54-
print "You pushed the button!"
66+
print "You pushed one of the buttons!"
67+
5568

5669
class TestFrame(wx.Frame):
5770
def __init__(self, app_logic, *args, **kwargs):
@@ -83,13 +96,12 @@ def __init__(self, app_logic, *args, **kwargs):
8396

8497
self.SetMenuBar(menuBar)
8598

86-
8799
def onOpen(self, evt=None):
88100
"""This method opens an existing file"""
89101
print "Open a file: "
90102
# 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
103+
# directory for the dialog, and no default file name is forced. This can easily
104+
# be changed in your program. This is an 'open' dialog, and allows multiple
93105
# file selections as well.
94106
#
95107
# Finally, if the directory is changed in the process of getting files, this
@@ -111,16 +123,11 @@ def onOpen(self, evt=None):
111123
self.app_logic.file_open( path )
112124
else :
113125
print "The file dialog was canceled before anything was selected"
114-
115-
116-
117126

118127
# Destroy the dialog. Don't do this until you are done with it!
119128
# BAD things can happen otherwise!
120129
dlg.Destroy()
121130

122-
123-
124131
def onClose(self, evt=None):
125132
print "close menu selected"
126133
self.app_logic.file_close()

week-09/code/basic_app_6b.py renamed to week-09/code/basic_app_7.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
"""
44
Example of the very basic, minimal framework for a wxPython application
55
6-
This version adds a BoxSizer for laying out buttons on the panel
6+
This adds a text box and a dialog box
77
"""
88

9+
910
import wx
1011

12+
#---------------------------------------------------------------------------
13+
14+
# This is how you pre-establish a file filter so that file dialogs
15+
# only show 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+
#---------------------------------------------------------------------------
1123

1224
class AppLogic(object):
1325
"""
@@ -85,10 +97,37 @@ def __init__(self, app_logic, *args, **kwargs):
8597

8698
self.SetMenuBar(menuBar)
8799

88-
89100
def onOpen(self, evt=None):
90-
print "open menu selected"
91-
self.app_logic.file_open()
101+
"""This method opens an existing file"""
102+
print "Open a file: "
103+
# Create the dialog. In this case the current directory is forced as the starting
104+
# directory for the dialog, and no default file name is forced. This can easily
105+
# be changed in your program. This is an 'open' dialog, and allows multiple
106+
# file selections as well.
107+
#
108+
# Finally, if the directory is changed in the process of getting files, this
109+
# dialog is set up to change the current working directory to the path chosen.
110+
dlg = wx.FileDialog(
111+
self, message="Choose a file",
112+
defaultDir=os.getcwd(),
113+
defaultFile="",
114+
wildcard=wildcard,
115+
style=wx.OPEN | wx.CHANGE_DIR
116+
)
117+
118+
# Show the dialog and retrieve the user response. If it is the OK response,
119+
# process the data.
120+
if dlg.ShowModal() == wx.ID_OK:
121+
# This returns a Python list of files that were selected.
122+
path = dlg.GetPath()
123+
print "I'd be opening file in onOpen ", path
124+
self.app_logic.file_open( path )
125+
else :
126+
print "The file dialog was canceled before anything was selected"
127+
128+
# Destroy the dialog. Don't do this until you are done with it!
129+
# BAD things can happen otherwise!
130+
dlg.Destroy()
92131

93132
def onClose(self, evt=None):
94133
print "close menu selected"

week-09/presentation-week09.pdf

-18 Bytes
Binary file not shown.

week-09/presentation-week09.tex

+3-9
Original file line numberDiff line numberDiff line change
@@ -648,17 +648,12 @@ \section{Basic Structure}
648648
%-------------------------------
649649
\begin{frame}[fragile]{Sizer Example}
650650

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 )` }
651+
{\Large The Basic \verb`BoxSizer`}\\
652+
{\large -- Lays out a row or column of controls...}
659653

660654
\vfill
661655
\begin{verbatim}
656+
Sizer.Add( window, proportion, flag, border )
662657
## do the layout
663658
S = wx.BoxSizer(wx.VERTICAL)
664659
@@ -667,7 +662,6 @@ \section{Basic Structure}
667662
668663
self.SetSizerAndFit(S)
669664
\end{verbatim}
670-
671665
\vfill
672666
code: \verb`code\basic_app_6.py`
673667
\end{frame}

0 commit comments

Comments
 (0)