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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

w.winfo_x()w.winfo_y()These methods return the x and y coordinates, respectively, of the top left corner of wrelative to its parent. If w has a border, this is the outer corner of the border.19. Standardizing appearance and the option databaseIt’s easy to apply colors, fonts, and other options to the widgets when you create them.However, if you want a lot of widgets to have the same background color or font, it’s tediousto specify each option each time, and it’s nice to let the user override your choices with their favorite color schemes, fonts,and other choices.Accordingly, we use the idea of an option database to set up default option values. Your application can specify a file (such as the standard .Xdefaults file used bythe X Window System) that contains the user’s p<strong>reference</strong>s. You can set up yourapplication to read the file and tell <strong>Tkinter</strong> to use those defaults. See the on the.option_readfile() method, above, in the section on Universal widget methods.See the section on Resource specification lines, below, <strong>for</strong> the structure of this file. Your application can directly specify defaults <strong>for</strong> one or many types of widgets byusing the .option_add() method; see this method in the section Universal widgetmethods, above.Be<strong>for</strong>e we discuss how options are set, consider the problem of customizing the appearanceof <strong>GUI</strong>s in general. We could give every widget in the application a name, andthen ask the user to specify every property of every name. But this is cumbersome, andwould also make the application hard to reconfigure—if the designer adds new widgets,the user would have to describe every property of every new widget.So, the option database allows the programmer and the user to specify general patternsdescribing which widgets to configure.These patterns operate on the names of the widgets, but widgets are named using twoparallel naming schemes: Every widget has a class name. By default, the class name is the same as the classconstructor: "Button" <strong>for</strong> buttons, "Frame" <strong>for</strong> a frame, and so on. However, youcan create new classes of widgets, usually inheriting from the Frame class, and givethem new names of your own creation. See the section How to name a widget class,below, <strong>for</strong> details. You can also give any widget an instance name. The default name of a widget isusually a meaningless number (e.g., "135147872" is a value that might come up inthe X implementation). However, as with widget classes, you can assign a name toany widget. See the section How to name a widget instance, below, <strong>for</strong> details.Every widget in every application there<strong>for</strong>e has two hierarchies of names—the class namehierarchy and the instance name hierarchy. For example, a button embedded in a textwidget which is itself embedded in a frame would have the class hierarchyNew Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Standardizing appearance Page 69

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

Saved successfully!

Ooh no, something went wrong!