Events and bindings
Events and bindings plays vital role in handling events in python.Widget configuration and styling is also vital in GUI.
To bind an event to any widget in python , bind() is used.
event can be button clicked or key press etc
handler can be type of button or key used to handle event
Whenever event occurs the handler is called to execute particular function related to the event.
Tkinter provides a powerful mechanism to deal with events. For each widget, you can bind python functions and methods to events. If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.
The event sequence is given as a string, using the following:
The type field is the essential part of an event specifier, whereas the “modifier” and “detail” fields are not obligatory and are left out in many cases. They are used to provide additional information for the chosen “type”. The event “type” describes the kind of event to be bound, e.g. actions like mouse clicks, key presses or the widget got the input focus.
Events and its Description
|A mouse button is pressed with the mouse pointer over the widget. If you press down a mouse button over a widget and keep it pressed, Tkinter will automatically "grab" the mouse pointer. Further mouse events like Motion and Release events will be sent to the current widget, even if the mouse is moved outside the current widget. The current position, relative to the widget, of the mouse pointer is provided in the x and y members of the event object passed to the callback. You can use ButtonPress instead of Button, or even leave it out completely: , , and <1> are all synonyms.|
|The mouse is moved with a mouse button being held down.The current position of the mouse pointer is provided in the x and y members of the event object passed to the callback, i.e. event.x, event.y|
|Event, if a button is released.
The current position of the mouse pointer is provided in the x and y members of the event object passed to the callback, i.e. event.x, event.y
|Similar to the Button event, see above, but the button is double clicked instead of a single click|
Summary of different events
Instance and Class bindings
The bind method we used in the above example creates an instance binding. This means that the binding applies to a single widget only; if you create new frames, they will not inherit the bindings.
But Tkinter also allows you to create bindings on the class and application level; in fact, you can create bindings on four different levels:
the widget instance, using bind.
the widget’s toplevel window (Toplevel or root), also using bind.
the widget class, using bind_class (this is used by Tkinter to provide standard bindings).
the whole application, using bind_all.
- To control the appearance of a widget, you usually use options rather than method calls.
- Typical options include text and color, size, command callbacks, etc.
- To deal with options, all core widgets implement the same configuration interface:
- Tkinter then uses the most recently created root window as master. Note that the name option can only be set when the widget is created.
cget(“option”) => string
- Return the current value of an option. Both the option name, and the returned value, are strings. To get the name option, use str(widget) instead.
- Most widgets allow you to specify the widget and text colors, using the background and foreground options.
- To specify a color, you can either use a color name, or explicitly specify the red, green, and blue (RGB) color components.
- Tkinter includes a color database which maps color names to the corresponding RGB values.
- This database includes common names like Red, Green, Blue, Yellow, and LightBlue, but also more exotic things like Moccasin, PeachPuff, etc.
- On an X window system, the color names are defined by the X server.
- You might be able to locate a file named xrgb.txt which contains a list of color names and the corresponding RGB values.
- Under Windows, you can also use the Windows system colors.
- Color names are case insensitive.
- Many (but not all) color names are also available with or without spaces between the words. For example, “lightblue”, “light blue”, and “Light Blue” all specify the same color.
- What is tkinter? GUI programming in Python and Python GUI Library
- Layout / Geometry Manager in Python
- Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
- Widgets in tkinter module – Python
- Canvas widget of tkinter module – Python
- Label, Text, Entry & Message Widget in Python
- Button, Checkbutton & Radiobutton Widget in Python
- Best post on Menu and Menubutton Widget of tkinter
- Best post- Listbox and Scrollbar(Slider) Widget-Python
- Best post on Frame Widget of tkinter in Python
- Best post on Spinbox and Scale Widget in Python
- Best post- LabelFrame, Toplevel, PanedWindow widgets
- Best post: message box widget and its methods
- Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
widgetclass(master, option=value, …) => widget
All Tkinter standard widgets provide a basic set of “styling” options, which allow you to modify things like colors, fonts, and other visual aspects of each widget.
You can also check following posts to know more about tkinter and GUI