Making the root window resizeable

Do you want to let the user resize your entire application window, and distribute the extra space among its internal widgets This requires some operations that are not obvious. It's necessary to use the techniques for row and column size management, described in Section 4.3, Configuring column and row sizes (p. 7), to make your Application widget's grid stretchable. However, that alone is not sufficient. Consider the trivial application discussed in Section 2, A minimal application (p. 3),...

A minimal application

Here is a trivial Tkinter program containing only a Quit button usr local bin python 1 from Tkinter import * 2 self.grid() 5 self.createWidgets() self.quitButton Button ( self, text 'Quit', app.master.title(Sample application) 9 1 This line makes the script self-executing, assuming that your system has the Python interpreter at path usr local bin python. 2 This line imports the entire Tkinter package into your program's namespace. 3 Your application class must inherit from Tkinter's Frame...

Canvas image objects

To display a graphics image on a canvas C, use id C.create_image ( x, y, option, ) This constructor returns the integer ID number of the image object for that canvas. The image is positioned relative to point (x, y). Options include Image to be displayed when the mouse is over the item. For option values, see image below. The default is anchor CENTER, meaning that the image is centered on the (x, y) position. See Section 5.5, Anchors (p. 11) for the possible values of this option. For example,...

Canvas arc objects

An arc object on a canvas, in its most general form, is a wedge-shaped slice taken out of an ellipse. This includes whole ellipses and circles as special cases. See Section 7.11, Canvas oval objects (p. 31) for more on the geometry of the ellipse drawn. To create an arc object on a canvas C, use id C.create_arc ( x0, y0, x1, y1, option, ) The constructor returns the object ID of the new arc object on canvas C. Point (x0, y0) is the top left corner and (x1, y1) the lower right corner of a...

Canvas bitmap objects

A bitmap object on a canvas is shown as two colors, the background color (for 0 data values) and the foreground color (for 1 values). To create a bitmap object on a canvas C, use id C.create_bitmap ( x, y, *options ) which returns the integer ID number of the image object for that canvas. The x and y values are the reference point that specifies where the bitmap is placed. Options include These options specify the background, bitmap, and foreground values when the bitmap is active, that is,...

Canvas line objects

In general, a line can consist of any number of segments connected end to end, and each segment can be straight or curved. To create a canvas line object on a canvas C, use id C.create_line ( x0, y0, x1, y1, , xn, yn, option, ) The line goes through the series of points (x0, y0), (x1, y1), (xn, yn). Options include These options specify the dash, fill, stipple, and width values to be used when the line is active, that is, when the mouse is over it. The default is for the line to have no...

Search pattern index option

Searches for pattern (which can be either a string or a regular expression) in the buffer starting at the given index. If it succeeds, it returns an index of the line. char form if it fails, it returns an empty string. The allowable options for this method are Set this option to True to search backwards from the index. Default is forwards. If you set this option to an IntVar control variable, when there is a match you can retrieve the length of the text that matched by using the .get() method...

Table of Contents

The .grid() 4.2. Other grid management 4.3. Configuring column and row 4.4. Making the root window 5. Standard 5.2. The coordinate 11 5.11. Window 5.12. Cap and join 5.13. Dash 5.14. Matching stipple 6. The Button 7. The Canvas 7.1. Canvas 7.2. The Canvas display 7.3. Canvas object 7.4. Canvas 7.5. Canvas tagOrld 7.6. Methods on Canvas 7.7. Canvas arc 7.8. Canvas bitmap 7.9. Canvas image 7.10. Canvas line 7.11. Canvas oval 7.12. Canvas polygon 7.13. Canvas...

The Button widget

To create a pushbutton in a top-level window or frame named parent w Button ( parent, option value, ) The constructor returns the new Button widget. Its options include The constructor returns the new Button widget. Its options include Background color when the button is under the cursor. Foreground color when the button is under the cursor. Where the text is positioned on the button. See Section 5.5, Anchors (p. 11). For example, anchor NE would position the text at the top right corner of the...

The Canvas widget

A canvas is a rectangular area intended for drawing pictures or other complex layouts. On it you can place graphics, text, widgets, or frames. See the following sections for methods that create objects on canvases .create_arc() A slice out of an ellipse. See Section 7.7, Canvas arc objects (p. 26). .create_bitmap() An image as a bitmap. See Section 7.8, Canvas bitmap objects (p. 28). .create_image() A graphic image. See Section 7.9, Canvas image objects (p. 29). .create_line() One or more line...

The Entry widget

The purpose of an Entry widget is to let the user see and modify a single line of text. If you want to display multiple lines of text that can be edited, see Section 23, The Text widget (p. 78). If you want to display one or more lines of text that cannot be modified by the user, see Section 11, The Label widget (p. 44). The selection is a highlighted region of the text in an Entry widget, if there is one. Typically the selection is made by the user with the mouse, and selected text is copied...

The Label widget

Label widgets can display one or more lines of text in the same style, or a bitmap or image. To create a label widget in a root window or frame parent The constructor returns the new Label widget. Options include The constructor returns the new Label widget. Options include Background color to be displayed when the mouse is over the widget. Foreground color to be displayed when the mouse is over the widget. This options controls where the text is positioned if the widget has more space than the...

The Label Frame widget

The LabelFrame widget, like the Frame widget, is a spatial container a rectangular area that can contain other widgets. However, unlike the Frame widget, the LabelFrame widget allows you to display a label as part of the border around the area. Here is an example of a LabelFrame widget containing two Button widgets. Note that the label Important controls interrupts the border. This widget illustrates the default GROOVE relief (see Section 5.6, Relief styles (p. 12)) and the default 'nw' label...

The Menu widget

Drop-down menus are a popular way to present the user with a number of choices, yet take up minimal space on the face of the application when the user is not making a choice. A menubutton is the part that always appears on the application. A menu is the list of choices that appears only after the user clicks on the menubutton. To select a choice, the user can drag the mouse from the menubutton down onto one of the choices. Alternatively, they can click and release the menubutton the choices...

The Menubutton widget

A menubutton is the part of a drop-down menu that stays on the screen all the time. Every menubutton is associated with a Menu widget (see above) that can display the choices for that menubutton when the user clicks on it. To create a menubutton within a root window or frame parent w Menubutton ( parent, option, ) The constructor returns the new Menubutton widget. Options The constructor returns the new Menubutton widget. Options The background color when the mouse is over the menubutton. See...

The Scrollbar widget

A number of widgets, such as listboxes and canvases, can act like sliding windows into a larger virtual area. You can connect scrollbar widgets to them to give the user a way to slide the view around relative to the contents. Here's a screen shot of an entry widget with an associated scrollbar widget Scrollbars can be horizontal, like the one shown above, or vertical. A widget that has two scrollable dimensions, such as a canvas or listbox, can have both a horizontal and a vertical scrollbar....

The Text widget

Text widgets are a much more generalized method for handling multiple lines of text than the Label widget. Text widgets are pretty much a complete text editor in a window You can mix text with different fonts, colors, and backgrounds. You can intersperse embedded images with text. An image is treated as a single character. See Section 23.3, Text widget images (p. 82). An index is a way of describing a specific position between two characters of a text widget. See Section 23.1, Text widget...

Tkinter reference a

I l yi I y LI l yi I Computer Center www.nmt.edu tcc Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc nmt.edu.

Wbind sequenceNone funcNone addNone

This method is used to attach an event binding to a widget. See Section 30, Events (p. 107) for the overview of event bindings. The sequence argument describes what event we expect, and the func argument is a function to be called when that event happens to the widget. If there was already a binding for that event for 10 this widget, normally the old callback is replaced with func, but you can preserve both callbacks by passing add + .

Writing your handlerThe Event class

The sections above tell you how to describe what events you want to handle, and how to bind them. Now let us turn to the writing of the handler that will be called when the event actually happens. The handler will be passed an Event object that describes what happened. The handler can be either a function or a method. Here is the calling sequence for a regular function The attributes of the Event object passed to the handler are described below. Some of these attributes are always set, but some...

The Scale widget

Tkinter 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....

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 tkMessage Box 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...

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...

The Spinbox widget

Tkinter Scrollbar

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....

The Option Menu widget

Tkinter Widgets Pulldown

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...

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...