12.07.2015 Views

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Frame.Text.ButtonIt might also have an instance hierarchy something like.mainFrame.messageText.panicButtonif you so named all the instances. The initial dot stands <strong>for</strong> the root window; see the.winfo_pathname() method in the section on universal widget methods, above, <strong>for</strong>more in<strong>for</strong>mation on pathnames.The option database mechanism can make use of either class names or instance names indefining options, so you can make options apply to whole classes (e.g., all buttons havea blue background) or to specific instances (e.g., the panic button has red letters on it).After we look at how to name classes and instances, in the section on Resource specificationlines, below, we’ll discuss how the options database really works.19.1 How to name a widget classFor example, suppose that Jukebox is a new widget class that you have created. It’sprobably best to have new widget classes inherit from the Frame class, so to <strong>Tkinter</strong> itacts like a frame, and you can arrange other widgets such as labels, entries, and buttonsinside it.You set the new widget’s class name by passing the name as the class_= attribute to theparent constructor in your new class’s constructor. Here is a fragment of the code thatdefines the new class:class Jukebox(Frame):def __init__(self, master):"""Constructor <strong>for</strong> the Jukebox class"""Frame.__init__ ( self, master, class_="Jukebox" )self.__createWidgets()...19.2 How to name a widget instanceTo give an instance name to a specific widget in your application, set that widget’s nameoption to a string containing the name.Here’s an example of an instance name. Suppose you are creating several buttons in anapplication, and you want one of the buttons to have an instance name of panicButton.Your call to the constructor might look like this:self.panic = Button ( self, name="panicButton", text="Panic", ...)19.3 Resource specification linesEach line in an option file specifies the value of one or more options in one or moreapplications and has one of these <strong>for</strong>mats:app option-pattern: valueoption-pattern: valueThe first <strong>for</strong>m sets options only when the name of the application matches app; the second<strong>for</strong>m sets options <strong>for</strong> all applications.For example, if your application is called xparrot, a line of the <strong>for</strong>mxparrot*background:LimeGreenNew Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Standardizing appearance Page 70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!