The Message widget
This widget is similar to the Label widget see Section 11, The Label widget p. 44 , but it is intended for displaying messages over multiple lines. All the text will be displayed in the same font if you need to display text with more than one font, see Section 23, The Text widget p. 78 . To create a new Message widget as the child of a root window or frame named parent This constructor returns the new Message widget. Options may be any of these This constructor returns the new Message widget....
The grid method
To display a widget w on your application screen w.grid option value, This method registers a widget w with the grid geometry manager if you don't do this, the widget will exist internally, but it will not be visible on the screen. Here are the options to the .grid geometry management method The column number where you want the widget gridded, counting from zero. The default value is zero. Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and...
Text widget tags
There are lots of ways to change both the appearance and functionality of the items in a text widget. For text, you can change the font, size, and color. Also, you can make text, widgets, or embedded images respond to keyboard or mouse actions. To control these appearance and functional features, you associate each feature with a tag. You can then associate a tag with any number of pieces of text in the widget. The name of a tag can be any string that does not contain white space or periods....
Toplevel menus
Especially under MacOS, it is sometimes desirable to create menus that are shown as part of the toplevel window. To do this, follow these steps. 1. Using any widget W, obtain the top-level window by using the W.winfo_toplevel method. 2. Create a Menu widget, using the top-level window as the first argument. 3. Items added to this Menu widget will be displayed across the top of the application. Here is a brief example. Assume that self is the application instance, an instance of a class that...
Scrolling an Entry widget
Making an Entry widget scrollable requires a little extra code on your part to adapt the Scrollbar widget's callback to the methods available on the Entry widget. Here are some code fragments illustrating the setup. First, the creation and linking of the Entry and Scrollbar widgets self.entry Entry self, width 10 self.entry.grid row 0, sticky E W self.entryScroll Scrollbar self, orient HORIZONTAL, self.entryScroll.grid row 1, sticky E W self.entry xscrollcommand self.entryScroll.set Here's the...
The Scrollbar command callback
When the user manipulates a scrollbar, the scrollbar calls its command callback. The arguments to this call depend on what the user does When the user requests a movement of one unit left or up, for example by clicking button B1 on the left or top arrowhead, the arguments to the callback look like command scroll, -1, units When the user requests a movement of one unit right or down, the arguments are command scroll, 1, units When the user requests a movement of one page left or up command...
Control variables the values behind the widgets
A Tkinter control variable is a special object that acts like a regular Python variable in that it is a container for a value, such as a number or string. One special quality of a control variable is that it can be shared by a number of different widgets, and the control variable can remember all the widgets that are currently sharing it. This means, in particular, that if your program stores a value v into a control variable c with its c.set v method, any widgets that are linked to that...
Virtual events
You can create your own new kinds of events called virtual events. You can give them any name you want so long as it is enclosed in double pairs of lt lt . For example, suppose you want to create a new event called lt lt panic gt gt , that is triggered either by mouse button 3 or by the pause key. To create this event, call this method on any widget w w.event_add panic , lt Button-3 gt , lt KeyPress-Pause gt You can then use panic in any event sequence. For example, if you use this call any...
metrics option
If you call this method with no arguments, it returns a dictionary of all the font metrics. You can retrieve the value of just one metric by passing its name as an argument. Metrics include Number of pixels of height between the baseline and the top of the highest ascender. Number of pixels of height between the baseline and the bottom of the lowest ascender. This value is 0 for a variable-width font and 1 for a monospaced font. Number of pixels of height total. This is the leading of type set...
The Scale widget
The purpose of a scale widget is to allow the user to set some integer or float value within a specified range. Here are two scale widgets, one horizontal and one vertical Each scale displays a slider that the user can drag along a trough to change the value. In the figure, the first slider is currently at -0.38 and the second at 7. You can drag the slider to a new value with mouse button 1. If you click button 1 in the trough, the slider will move one increment in that direction per click....
scanmark x y
This method is used to implement fast scrolling of a canvas. The intent is that the user will press and hold a mouse button, then move the mouse up to scan scroll the canvas horizontally and vertically in that direction at a rate that depends on how far the mouse has moved since the mouse button was depressed. To implement this feature, bind the mouse's button-down event to a handler that calls scan_mark x, y where x and y are the current mouse coordinates. Bind the lt Motion gt event to a...
The Listbox widget
The purpose of a listbox widget is to display a set of lines of text. Generally they are intended to allow the user to select one or more items from a list. All the lines of text use the same font. If you need something more like a text editor, see Section 23, The Text widget p. 78 . To create a new listbox widget inside a root window or frame parent w Listbox parent, option, This constructor returns the new Listbox widget. Options This constructor returns the new Listbox widget. Options This...
The PanedWindow widget
The purpose of the PanedWindow widget is to give the application's user some control over how space is divided up within the application. A PanedWindow is somewhat like a Frame it is a container for child widgets. Each PanedWindow widget contains a horizontal or vertical stack of child widgets. Using the mouse, the user can drag the boundaries between the child widgets back and forth. You may choose to display handles within the widget. A handle is a small square that the user can drag with the...
How to name a widget class
For example, suppose that Jukebox is a new widget class that you have created. It's probably best to have new widget classes inherit from the Frame class, so to Tkinter it acts like a frame, and you can arrange other widgets such as labels, entries, and buttons inside it. You set the new widget's class name by passing the name as the class_ attribute to the parent constructor in your new class's constructor. Here is a fragment of the code that defines the new class Constructor for the Jukebox...
Standardizing appearance and the option database
It'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 tedious to 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 by the...
Canvas rectangle objects
Each rectangle is specified as two points x0, y0 is the top left corner, and x1, y1 is the location of the pixel just outside of the bottom right corner. For example, the rectangle specified by top left corner 100,100 and bottom right corner 102,102 is a square two pixels by two pixels, including pixel 101,101 but not including 102,102 . The outline lies inside the rectangle on its top and left sides, but outside the rectangle on its bottom and right side. The default appearance is a...
The tkMessageBox dialogs module
Once you import the tkMessageBox module, you can create any of these seven common types of popup menu by calling functions from this table. .askokcancel titZe, message, options .askquestion title, message, options Ask for a date one more time .askretrycancel title, message, options Can you cut down a tree 4f- with a herring showinfo X yThis is an ex-parrot. Your brain may not be the boss .showwarning title, message, options In each case, the title is a string to be displayed in the top of the...
dash
This option is specified as a tuple of integers. The first integer specifies how many pixels should be drawn. The second integer specifies how many pixels should be skipped before starting to draw again, and so on. When all the integers in the tuple are exhausted, they are reused in the same order until the border is complete. For example, dash 3,5 produces alternating 3-pixel dashes separated by 5-pixel gaps. A value of dash 7,1,1,1 produces a dash-and-dot pattern, with the dash seven times as...
The extra arguments trick
Sometimes you would like to pass other arguments to a handler besides the event. Here is an example. Suppose your application has an array of ten checkbuttons whose widgets are stored in a list self.cbList, indexed by the checkbutton number in range 10 . Suppose further that you want to write one handler named .__cbHandler for lt Button-1 gt events in all ten of these checkbuttons. The handler can get the actual Checkbutton widget that triggered it by referring to the .widget attribute of the...
Canvas polygon objects
As displayed, a polygon has two parts its outline and its interior. Its geometry is specified as a series of vertices x0, y0 , x1, y1 , xn, yn , but the actual perimeter includes one more segment from xn, yn back to x0, y0 . In this example, there are five vertices id C.create_polygon x0, y0, x1, y1, , option, The constructor returns the object ID for that object. Options These options specify the appearance of the polygon when it is in the ACTIVE state, that is, when the mouse is over it. For...
Matching stipple patterns
This may seem like an incredibly picky style point, but if you draw a graphic that has two objects with stippled patterns, a real professional will make sure that the patterns align along their boundary. Here is an example. The left-hand screen shot shows two adjacent 100x100 squares stippled with the gray12 pattern, but the right-hand square is offset vertically by one pixel. The short black line in the center of the figure is drawn along the boundary of the two figures. The second screen shot...
The Spinbox widget
The Spinbox widget allows the user to select values from a given set. The values may be a range of numbers, or a fixed set of strings. On the screen, a Spinbox has an area for displaying the current values, and a pair of arrowheads. The user can click the upward-pointing arrowhead to advance the value to the next higher value in sequence. If the value is already at maximum, you can set up the widget, if you wish, so that the new value will wrap around to the lowest value. The user can click the...
Canvas oval objects
Ovals, mathematically, are ellipses, including circles as a special case. The ellipse is fit into a rectangle defined by the coordinates x0, y0 of the top left corner and the coordinates x1, y1 of the bottom right corner The oval will coincide with the top and left-hand lines of this box, but will fit just inside the bottom and right-hand sides. To create an ellipse on a canvas C, use id C.create_oval x0, y0, x1, y1, option, which returns the object ID of the new oval object on canvas C....
scanmark x
Use this option to set up fast scanning of the contents of the Entry widget that has a scrollbar that supports horizontal scrolling. To implement this feature, bind the mouse's button-down event to a handler that calls scan_mark x , where x is the current mouse x position. Then bind the lt Motion gt event to a handler that calls scan_dragto x , where x is the current mouse x position. The scan_dragto method scrolls the contents of the Entry widget continuously at a rate proportional to the...
Setting tabs in a Text widget
The tabs option for Text widgets gives you a number of ways to set tab stops within the widget. The default is to place tabs every eight characters. To set specific tab stops, set this option to a sequence of one or more distances. For example, setting tabs 3c, 5c, 12c would put tab stops 3, 5, and 12cm from the left side. Past the last tab you set, tabs have the same width as the distance between the last two existing tab stops. So, continuing our example, because 12c-5c is 7 cm, if the user...
The OptionMenu widget
The purpose of this widget is to offer a fixed set of choices to the user in a drop-down menu. The illustrations above shows an OptionMenu in two states. The left-hand example shows the widget in its initial form. The right-hand example shows how it looks when the mouse has clicked on it and dragged down to the boat choice. To create a new OptionMenu widget as the child of a root window or frame named parent w OptionMenu parent, variable, choice1t choice2, This constructor returns the new...
Event types
The full set of event types is rather large, but a lot of them are not commonly used. Here are most of the ones you'll need A widget is changing from being inactive to being active. This refers to changes in the state option of a widget such as a button changing from inactive grayed out to active. The user pressed one of the mouse buttons. The detail part specifies which button. For mouse wheel support under Linux, use Button-4 scroll up and Button-5 scroll down . Under Linux, your handler for...
PanedWindow child configuration options
Each child of a PanedWindow has a set of configuration options that control its position and appearance. These options can be provided when a child is added with the .add method, or set with the .pan-econfig method, or queried with the .panecget methods described above. Normally, when you .add a new child to a PanedWindow, the new child is added after any existing child widgets. You may instead use the after w option to insert the new widget at a position just after an existing child widget w....
Cap and join styles
For pleasant and effective rendering of diagrams, sometimes it is a good idea to pay attention to cap and join styles. The cap style of a line is the shape of the end of the line. Styles are BUTT The end of the line is cut off square at a line that passes through the endpoint. PROJECTING The end of the line is cut off square, but the cut line projects past the endpoint a distance equal to half the line's width. ROUND The end describes a semicircle centered on the endpoint. The join style...
identify x y
For a given location x, y in window coordinates, this method returns a value that describes the feature at that location. If the feature is a child window, the method returns an empty string. If the feature is a sash, the method returns a tuple n, 'sash' where n is 0 for the first sash, 1 for the second, and so on. If the feature is a handle, the method returns a tuple n, 'handle' where n is 0 for the first handle, 1 for the second, and so on.







