[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
photos = 'ora-lp4e.gif', 'pythonPowered.gif', 'python_conf_ora.gif'
self.toolPhotoObjs = []
for file in photos:
img = PhotoImage(file=imgdir + file)
btn = Button(toolbar, image=img, command=self.greeting)
btn.config(bd=5, relief=RIDGE)
btn.config(width=size[0], height=size[1])
btn.pack(side=LEFT)
self.toolPhotoObjs.append(img) # keep a reference
Button(toolbar, text='Quit', command=self.quit).pack(side=RIGHT, fill=Y)

Figure 9-12. menuDemo2: images in the toolbar with PIL


When run, this alternative uses GIF images, and renders the window grabbed in Fig-
ure 9-13. Depending on your user’s preferences, you might want to resize the GIF
images used here for this role with other tools; we only get part of unresized photos in
the fixed-width buttons, which may or may not be enough.


As is, this is something of a first cut solution to toolbar image buttons. There are many
ways to configure such image buttons. Since we’re going to see PIL in action again later
in this chapter when we explore canvases, though, we’ll leave further extensions in the
suggested exercise column.


Automating menu construction


Menus are a powerful tkinter interface device. If you’re like me, though, the examples
in this section probably seem like a lot of work. Menu construction can be both code
intensive and error prone if done by calling tkinter methods directly. A better approach
might automatically build and link up menus from a higher-level description of their
contents. In fact, in Chapter 10, we’ll meet a tool called GuiMixin that automates the
menu construction process, given a data structure that contains all menus desired. As


Menus | 521
Free download pdf