Next / Previous / Contents / TCC Help System / NM Tech homepage

19. Universal widget methods

The methods are defined below on all widgets. In the descriptions, w can be any widget—a frame, a top-level window, whatever.

w.after ( ms, func=None, *args )

Requests Tkinter to call function func with arguments args after a delay of at least ms milliseconds. There is no upper limit to how long it will actually take, but your callback won't be called sooner than you request, and it will be called only once.

This method returns an integer “after identifier” that can be passed to the .after_cancel() method if you want to cancel the callback.

w.after_cancel ( id )

Cancels a request for callback set up earlier .after(). The id argument is the result returned by the original .after() call.

w.after_idle ( func, *args )

Requests that Tkinter call function func with arguments args next time the system is idle, that is, next time there are no events to be processed. The callback will be called only once.

w.bell()

Makes a noise, usually a beep.

w.bind ( sequence=None, func=None, add=None )

This method is used to attach an event binding to a widget. See Section 24, “Events” 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 this widget, normally the old callback is replaced with func, but you can preserve both callbacks by passing add="+".

w.bind_all ( sequence=None, func=None, add=None )

Like .bind(), but applies to all widgets in the entire application.

w.bind_class ( className, sequence=None, func=None, add=None )

Like .bind(), but applies to all widgets named className (e.g., "Button").

w.bindtags ( tagList=None )

If you call this method, it will return the “binding tags” for the widget as a sequence of strings. A binding tag is the name of a window (starting with ".") or the name of a class (e.g., "Listbox").

You can change the order in which binding levels are called by passing as an argument the sequence of binding tags you want the widget to use.

See Section 24, “Events” for a discussion of binding levels and their relationship to tags.

w.cget ( option )

Returns the current value of option as a string. You can also get the value of an option for widget w as w[option].

w.clipboard_append ( text )

Appends the given text string to the display's clipboard, where cut and pasted strings are stored for all that display's applications.

w.clipboard_clear()

Clears the display's clipboard (see .clipboard_append() above).

w.config( option=value, ... )

Same as .configure().

w.configure ( option=value, ... )

Set the values of one or more options. For the options whose names are Python reserved words (class, from, in), use a trailing underbar: "class_", "from_", "in_".

You can also set the value of an option for widget w with the statement

    w[option] = value

If you call the .config() method on a widget with no arguments, you'll get a dictionary of all the widget's current options. The keys are the option names (including aliases like bd for borderwidth). The value for each key is:

  • for most entries, a five-tuple: (option name, option database key, option database class, default value, current value); or,

  • for alias names (like "fg"), a two-tuple: (alias name, equivalent standard name).

w.destroy()

Calling w.destroy() on a widget w destroys w and all its children.

w.event_add ( virtual, *sequences )

This method creates a virtual event whose name is given by the virtual string argument. Each additional argument describes one sequence, that is, the description of a physical event. When that event occurs, the new virtual event is triggered.

See Section 24, “Events” for a general description of virtual events.

w.event_delete ( virtual, *sequences )

Deletes physical events from the virtual event whose name is given by the string virtual. If all the physical events are removed from a given virtual event, that virtual event won't happen anymore.

w.event_generate ( sequence, **kw )

This method causes an event to trigger without any external stimulus. The handling of the event is the same as if it had been triggered by an external stimulus. The sequence argument describes the event to be triggered. You can set values for selected fields in the Event object by providing keyword=value arguments, where the keyword specifies the name of a field in the Event object.

See Section 24, “Events” for a full discussion of events.

w.event_info ( virtual=None )

If you call this method without an argument, you'll get back a sequence of all the currently defined virtual event names.

To retrieve the physical events associated with a virtual event, pass this method the name of the virtual event and you will get back a sequence of the physical sequence names, or None if the given virtual event has never been defined.

w.focus_displayof()

Returns the name of the window that currently has input focus on the same display as the widget. If no such window has input focus, returns None.

See Section 23, “Focus: routing keyboard input” for a general description of input focus.

w.focus_force()

Force the input focus to the widget. This is impolite. It's better to wait for the window manager to give you the focus. See also .grab_set_global() below.

w.focus_get()

Get the name of the widget that has focus in this application, if any—otherwise return None.

w.focus_lastfor()

This method retrieves the name of the widget that last had the input focus in the top-level window that contains w. If none of this top-level's widgets have ever had input focus, it returns the name of the top-level widget. If this application doesn't have the input focus, .focus_lastfor() will return the name of the widget that will get the focus next time it comes back to this application.

w.focus_set()

If w's application has the input focus, the focus will jump to w. If w's application doesn't have focus, Tk will remember to give it to w next the application gets focus.

w.grab_current()

If there is a grab in force for w's display, return its identifier, otherwise return None. Refer to Section 24, “Events” for a discussion of grabs.

w.grab_release()

If w has a grab in force, release it.

w.grab_set()

Widget w grabs all events for w's application. If there was another grab in force, it goes away. See Section 24, “Events” for a discussion of grabs.

w.grab_set_global()

Widget w grabs all events for the entire screen. This is considered impolite and should be used only in great need. Any other grab in force goes away. Try to use this awesome power only for the forces of good, and never for the forces of evil, okay?

w.grab_status()

If there is a local grab in force (set by .grab_set()), this method returns the string "local". If there is a global grab in force (from .grab_set_global()), it returns "global". If no grab is in force, it returns None.

w.image_names()

Returns the names of all the images in w's application as a sequence of strings.

w.keys()

Returns the option names for the widget as a sequence of strings.

w.mainloop()

This method must be called, generally after all the static widgets are created, to start processing events. You can leave the main loop with the .quit() method (below). You can also call this method inside an event handler to resume the main loop.

w.nametowidget ( name )

This method returns the actual widget whose path name is name. See Section 4.11, “Window names”.

w.option_add ( pattern, value, priority=None )

This method adds default option values to the Tkinter option database. The pattern is a string that specifies a default value for options of one or more widgets. The priority values are one of:

widgetDefaultLevel 20, for global default properties of widgets.
startupFileLevel 40, for default properties of specific applications.
userDefaultLevel 60, for options that come from user files such as their .Xdefaults file.
interactiveLevel 80, for options that are set after the application starts up. This is the default priority level.

Higher-level priorities take precedence over lower-level ones. See Section 20, “Standardizing appearance” for an overview of the option database. The syntax of the pattern argument to .option_add() is the same as the option-pattern part of the resource specification line.

For example, to get the effect of this resource specification line:

*Button*font: times 24 bold

your application (self in this example) might include these lines:

    self.bigFont = tkFont.Font ( family="times", size=24,
                                 weight="bold" )
    self.option_add ( "*Button*font", self.bigFont )

Any Button widgets created after executing these lines would default to bold Times 24 font (unless overriden by a font option to the Button constructor).

w.option_clear()

This method removes all options from the Tkinter option database. This has the effect of going back to all the default values.

w.option_get ( name, classname )

Use this method to retrieve the current value of an option from the Tkinter option database. The first argument is the instance key and the second argument is the class key. If there are any matches, it returns the value of the option that best matches. If there are no matches, it returns "".

Refer to Section 20, “Standardizing appearance” for more about how keys are matched with options.

w.option_readfile ( fileName, priority=None )

As a convenience for user configuration, you can designate a named file where users can put their preferred options, using the same format as the .Xdefaults file. Then, when your application is initializing, you can pass that file's name to this method, and the options from that file will be added to the database. If the file doesn't exist, or its format is invalid, this method will raise TclError.

Refer to Section 20, “Standardizing appearance” for an introduction to the options database and the format of option files.

w.quit()

This method exits the main loop. See .mainloop(), above, for a discussion of main loops.

w.selection_clear()

If w currently has a selection (such as a highlighted segment of text in an entry widget), clear that selection.

w.selection_get()

If w currently has a selection, this method returns the selected text. If there is no selection, it raises TclError.

w.selection_own()

Make w the owner of the selection in w's display, stealing it from the previous owner, if any.

w.selection_own_get()

Returns the widget that currently owns the selection in w's display. Raises TclError if there is no such selection.

w.tk_focusFollowsMouse()

Normally, the input focus cycles through a sequence of widgets determined by their hierarchy and creation order; see Section 23, “Focus: routing keyboard input”. You can, instead, tell Tkinter to force the focus to be wherever the mouse is; just call this method. There is no easy way to undo it, however.

w.unbind ( sequence, funcid=None )

This method deletes bindings on w for the event described by sequence. If the second argument is a callback bound to that sequence, that callback is removed and the rest, if any, are left in place. If the second argument is omitted, all bindings are deleted.

See Section 24, “Events”, below, for a general discussion of event bindings.

w.unbind_all ( sequence )

Deletes all event bindings throughout the application for the event described by the given sequence.

w.unbind_class ( className, sequence )

Like .unbind(), but applies to all widgets named className (e.g., "Entry" or "Listbox").

w.update()

This method forces the updating of the display. It should be used only if you know what you're doing, since it can lead to unpredictable behavior or looping. It should never be called from an event callback or a function that is called from an event callback.

w.update_idletasks()

Some tasks in updating the display, such as resizing and redrawing widgets, are called idle tasks because they are usually deferred until the application has finished handling events and has gone back to the main loop to wait for new events.

If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any widget.

w.winfo_children()

Returns a list of all w's children, in their stacking order from lowest (bottom) to highest (top).

w.winfo_class()

Returns w's class name (e.g., "Button").

w.winfo_colormapfull()

Returns true if w's window's color map is full, that is, if the last attempt to add a color to it failed and no colors have been removed since then.

w.winfo_containing ( rootX, rootY, displayof=0 )

This method is used to find the window that contains point (rootX, rootY). If the displayof option is false, the coordinates are relative to the application's root window; if true, the coordinates are treated as relative to the top-level window that contains w. If the specified point is in one of the application's top-level window, this method returns that window; otherwise it returns None.

w.winfo_depth()

Returns the number of bits per pixel in w's display.

w.winfo_fpixels ( number )

For any dimension number (see Section 4.1, “Dimensions”), this method returns that distance in pixels on w's display, as a floating-point number.

w.winfo_geometry()

Returns the geometry string describing the size and on-screen location of w. See Section 4.10, “Geometry strings”.

Warning

The geometry is not accurate until the application has updated its idle tasks. In particular, all geometries are initially "1x1+0+0" until the widgets and geometry manager have negotiated their sizes and positions. See the .update_idletasks() method, above, in this section to see how to insure that the widget's geometry is up to date.

w.winfo_height()

Returns the current height of w in pixels. See the remarks on geometry updating under .winfo_geometry(), above.

w.winfo_id()

Returns an integer that uniquely identifies w within its top-level window. You will need this for the .winfo_pathname() method, below.

w.winfo_ismapped()

This method returns true if w is mapped, false otherwise. A widget is mapped if it has been gridded (or placed or packed, if you are using one of the other geometry managers) into its parent, and if its parent is mapped, and so on up to the top-level window.

w.winfo_manager()

If w has not been gridded (or placed via one of the other geometry managers), this method returns an empty string. If w has been gridded or otherwise placed, it returns a string naming the geometry manager, such as "grid".

w.winfo_name()

This method returns w's name relative to its parent. See Section 4.11, “Window names”. Also see .winfo_pathname(), below, to find out how to obtain a widget's path name.

w.winfo_parent()

Returns w's parent's path name, or an empty string if w is a top-level window. See Section 4.11, “Window names” above, for more on widget path names.

w.winfo_pathname ( id, displayof=0 )

If the displayof argument is false, returns the window path name of the widget with unique identifier id in the application's main window. If displayof is true, the id number specifies a widget in the same top-level window as w. See Section 4.11, “Window names” for a discussion of widget path names.

w.winfo_pixels ( number )

For any dimension number (see Dimensions, above), this method returns that distance in pixels on w's display, as an integer.

w.winfo_pointerx()

Returns the same value as the x coordinate returned by .winfo_pointerxy().

w.winfo_pointery()

Returns the same value as the y coordinate returned by .winfo_pointerxy().

w.winfo_pointerxy()

Returns a tuple (x, y) containing the coordinates of the mouse pointer relative to w's root window. If the mouse pointer isn't on the same screen, returns (-1, -1).

w.winfo_reqheight()

These methods return the requested height of widget w. This is the minimum height necessary so that all of w's contents have the room they need. The actual height may be different due to negotiations with the geometry manager.

w.winfo_reqwidth()

Returns the requested width of widget w, the minimum width necessary to contain w. As with .winfo_reqheight(), the actual width may be different due to negotiations with the geometry manager.

w.winfo_rgb ( color )

For any given color, this method returns the equivalent red-green-blue color specification as a 3-tuple (r, g, b), where each number is an integer in the range [0, 65536). For example, if the color is "green", this method returns the 3-tuple (0, 65535, 0).

For more on specifying colors, see Section 4.3, “Colors”.

w.winfo_rootx()

Returns the x coordinates of the left-hand side of w's root window relative to w's parent.

If w has a border, this is the outer edge of the border.

w.winfo_rooty()

Returns the y coordinate of the top side of w's root window relative to w's parent.

If w has a border, this is the top edge of the border.

w.winfo_screenheight()

Returns the height of the screen in pixels.

w.winfo_screenwidth()

Returns the width of the screen in pixels.

w.winfo_screenmmheight()

Returns the height of the screen in millimeters.

w.winfo_screenmmwidth()

Returns the width of the screen in millimeters.

w.winfo_screenvisual()

Returns a string that describes the display's method of color rendition. This is usually "truecolor" for 16- or 24-bit displays, "pseudocolor" for 256-color displays.

w.winfo_toplevel()

Returns the top-level window containing w. That window supports all the methods on Toplevel widgets; see Section 18, “Toplevel: Top-level window methods”.

w.winfo_width()

Returns the current width of w in pixels. See the remarks on geometry updating under .winfo_geometry(), above.

w.winfo_x()

Returns the x coordinate of the left side of w relative to its parent. If w has a border, this is the outer edge of the border.

w.winfo_y()

Returns the y coordinate of the top side of w relative to its parent. If w has a border, this is the outer edge of the border.