Name:
[002] Ian Ozsvald
Member:
58 months
Authored:
181 videos
Description:
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_:
.. image:: http://procasts.co.uk/media/procasts_sma ...
Add Menus, wx Ids, Binding [ID:528] (4/14)
in series: Build a wxPython Image Viewer
video tutorial by Ian Ozsvald, added 02/08
Name:
[002] Ian Ozsvald
Member:
58 months
Authored:
181 videos
Description:
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production ...
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
We add a MenuBar with some Menu items. When adding Menus or other widgets you always have to specify an 'id' and there are multiple (slightly confusing!) ways of creating them...to set our wx Ids we discuss wx's '-1', wx.ID_ANY, wx.NewId() and managing your own Ids. I provide the heuristic of always using '-1' for your Ids unless you need more control.
We test wx's new Id using object.GetId() and we use Wingware's Source Assistant feature to look at some wx help text.
Menus aren't much use if they can't control anything so we move on to using self.Bind(...) to attach a menu event to a function - and we look at what happens if the specified function doesn't exist when we try to Bind to it.
Finally we establish the self.OnExit() method and use self.Destory() to cleanly exit the application. We also look at using 'print' to follow the flow of events in the application.
import wx
MAIN_WINDOW_DEFAULT_SIZE = (300,200)
class Frame(wx.Frame):
def __init__(self, parent, id, title):
style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER) # XOR to remove the resizeable border
wx.Frame.__init__(self, parent, id, title=title, size=MAIN_WINDOW_DEFAULT_SIZE, style=style)
self.Center() # open in the centre of the screen
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour('White') # make the background of the window white
self.CreateMenuBar()
def CreateMenuBar(self):
"Create a menu bar with Open, Exit items"
menuBar = wx.MenuBar()
# Tell our Frame about this MenuBar
self.SetMenuBar(menuBar)
menuFile = wx.Menu()
menuBar.Append(menuFile, '&File')
# NOTE on wx ids - they're used everywhere, we don't care about them
# Used to handle events and other things
# An id can be -1 or wx.ID_ANY, wx.NewId(), your own id
# Get the id using object.GetId()
#fileOpenMenuItem = menuFile.Append(-1, '&Open Image', 'Open a picture')
#print "fileOpenMenuItem.GetId()", fileOpenMenuItem.GetId()
#self.Bind(wx.EVT_MENU, self.OnOpen, fileOpenMenuItem)
# add a 'mirror' option, disable it for now
# we add mirrorMenuItem to self so that we can reference it later
#self.mirrorMenuItem = menuFile.Append(-1, '&Mirror Image', 'Mirror the image horizontally')
#self.mirrorMenuItem.Enable(False) # we can't mirror an image until we've loaded one in, so start with 'mirror' disabled
#self.Bind(wx.EVT_MENU, self.OnMirrorImage, self.mirrorMenuItem)
# create a menu item for Exit and bind it to the OnExit function
#exitMenuItem = menuFile.Append(-1, 'E&xit', 'Exit the viewer')
#self.Bind(wx.EVT_MENU, self.OnExit, exitMenuItem)
# add a Help menu with an About item
#menuHelp = wx.Menu()
#menuBar.Append(menuHelp, '&Help')
#helpMenuItem = menuHelp.Append(-1, '&About', 'About screen')
#self.Bind(wx.EVT_MENU, self.OnAbout, helpMenuItem)
def OnOpen(self, event):
print "OnOpen called"
#def OnExit(self, event):
# "Close the application by Destroying the object"
# # TRY add in your own print call here
# self.Destroy() # SHOW HELP SIDEBAR
class App(wx.App):
def OnInit(self):
self.frame = Frame(parent=None, id=-1, title='Image Viewer')
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == "__main__":
# make an App object, set stdout to the console so we can see errors
app = App(redirect=False)
app.MainLoop()
- python
- beginner_programming
- programs
- episodes
- application
- learn
- text
- help
- source
- look
- builds
- information
- GUI
- IDE
- objects
- images
- features
- wxpython
- end
- functions
- t
- control
- programmers
- background
- set
- test
- knowledge
- widgets
- exercise
- little
- wing
- wingware
- fully-worked
- menu
- items
- platforms
- source_assistant
- exercise_solution
- self
Got any questions?
Get answers in the ShowMeDo Learners Google Group.
Video statistics:
- Video's rank shown in the most popular listing
- Video plays: 160 (since July 30th)
- Plays in last week: 1
- Published: 30 months ago
Thank-yous, questions and comments
If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?
You may also want to see our ShowMeDo Google Group to speak to our active users and authors.
All comments excluding tick-boxed quick-comments
Great video.
I had mentioned in an earlier comment I could not see the source code (the code to cut and paste out for my own use). This seems to be an issue when using Internet explorer 7 (on Vista). But switching to firefox the source code section does display. It also displays properly on Opera. I wish I knew why it does not show properly on IE, but that is not a big issue. Really like the video's, speed is fine, I hope you are getting paid by Wingware as you have me thinking about paying for their IDE.
Useful screencast, I think that the event IDs managed by wxpython is a good idea. Worrying about it at beginner fases would be clutering.
It seems my problems have gone away . . . :-)
the code from episode 4 runs just fine.
Great stuff.
Hi Lucas. There was no particular reason, I had refactored the code several times and I think I just missed this one. Out of preference I'd have used 'fileOpenMenuItem' with 'fileExitMenuItem' OR 'openMenuItem' and 'exitMenuItem', but I guess this naming just slipped through.
Well spotted :-)
Ian.
Hey Ian,
after rewatching this video, I noticed one thing while playing around with the source code:
you named the "Open" item in the "File" menu
fileOpenMenuItem
The "Exit" item in the same "File" menu is called
exitMenuItem
The "file" prefix makes sense, it signifies that the menu item is part of the file menu.
Why did you decide not to prefix the "exitMenuItem" menu item?
Lucas
Good job Ian! This video really repays playing over more than once. I like the way that you are building up the code by assembling the parts and explaining them as you go along. wxPython is a lot more complicated than regular Python, and can be confusing if the viewer tries to take in too much as once. By the way, the viewer should be aware that you can use IPython to supply docstrings:
import wx
wx.Frame.Destroy??
(although Wing is more convenient).
Jerol
This is a great series. Start to finish dev on a simple app.
Looking forward to the next installment.
Video published, thanks for contributing to ShowMeDo
