Events and Bindings in Python along with Widget configuration and styling

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

widget.bind(event,handler)

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


Example

OUTPUT

bind 1

Events

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:
Syntax

(modifier-type-detail)

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

EventsDescription
butttonA 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.
motionThe 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
BrEvent, 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

db
Similar to the Button event, see above, but the button is double clicked instead of a single click

Summary of different events

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.

Example

motionevents 2

OUTPUT

motionoutput 1

Widget Configuration

Leave a Comment