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

(yzsuai) #1

Example 1-26. PP4E\Preview\attachgui.py


from tkinter import *
from tkinter102 import MyGui


main app window


mainwin = Tk()
Label(mainwin, text=name).pack()


popup window


popup = Toplevel()
Label(popup, text='Attach').pack(side=LEFT)
MyGui(popup).pack(side=RIGHT) # attach my frame
mainwin.mainloop()


This example attaches our one-button GUI to a larger window, here a Toplevel pop-
up window created by the importing application and passed into the construction call
as the explicit parent (you will also get a Tk main window; as we’ll learn later, you always
do, whether it is made explicit in your code or not). Our one-button widget package is
attached to the right side of its container this time. If you run this live, you’ll get the
scene captured in Figure 1-4; the “press” button is our attached custom Frame.


Figure 1-4. Attaching GUIs


Moreover, because MyGui is coded as a class, the GUI can be customized by the usual
inheritance mechanism; simply define a subclass that replaces the parts that differ. The
reply method, for example, can be customized this way to do something unique, as
demonstrated in Example 1-27.


Example 1-27. PP4E\Preview\customizegui.py


from tkinter import mainloop
from tkinter.messagebox import showinfo
from tkinter102 import MyGui


Step 5: Adding a GUI | 43
Free download pdf