Nictk 2.2.0
An easy to use pyton GUI
Events and callbacks

In tkinter we have two ways to connect a callback to a widget event:

  • The bind() method, which allows the user to freely bind an event generated by the widget to a callback function.
  • The command option of the config() method which allows to bind a predefined event (the click for a button, the return for an Entry, ecc) to a callback function This can be used also in the widget constructor.

The two methods differ: with bind() the callback gets a tkinter.Event as parameter, i.e. an object with various informations on what is happened, while with command the callback gets no parameter. So tkinter suggest to write for the callbacks the signature:

def callback(event=None):

If you are not familiar with tkinter events, I suggest the following documentation:

In Nictk I made these changes:

  • Both the bind() method and the command option pass a tkinter.Event object to the callback. In the case of the command the event is a <<VirtualEvent>> and has only the following attributes:
    • .widget
    • .x
    • .y
    • .value (see below)
  • I added the command option to some other widgets: this is a list of the widgets which support it (in the constructor and in the config() method):
  • For the command option you have the following choices:
    • command = callback this only binds the callback to the event
    • command = (callback, value) setting a tuple callback - value the Event passed to the callback will contain the given value in the .value attribute (otherwise this will be set to None)

The callback signature

So in Nictk a callback will always receive the event parameter. His signature should be

def callback(event):

for a non member function or

def callback(self, event):

for a class member called within the class (this can be useful when subclassing a Nictk widget)

Examples of these procedures are in all example files.

Events and disabled widgets

Another strange behavior of tkinter is the following: when you bind an event to a widget and disable the widget the event will be ignored only if it was bound with the command option, while if it was bound with the bind() method the disabled widget will continue to respond to the event. In Nictk I changed this (since version 2.1.0), so that disabled widgets don't respond to events regardless of the method with which they were bound.