Skip to content
Alejandro Autalán edited this page Jul 31, 2022 · 2 revisions

Command Properties

Some tk widgets have command properties. These properties accept a function callback that will be called when the user interacts with the widget.

Using callbacks in pygubu

To define a callback for a widget simply enter the name of the function (or method) in the command property editor.

In code, you must call the method connect_callbacks of the builder object, specifying which functions to connect.

You can pass a dictionary of key:value pairs, where the key is the callback name as was specified on the UI definition, and value is the real python function.

Alternatively, you can pass an object with the same method names as defined in the UI file, and the connection will be made automatically.

Example 1: Using function callbacks

Configure the callback names in the UI definition:

Set 1° button callback name:

command_001

Set 2° button callback name:

command_002

Set 3° button callback name:

command_003

Example dir: command_properties

File: command_properties.py

    ...

def on_button1_click():
    messagebox.showinfo("Message", "You clicked Button 1")


def on_button2_click():
    messagebox.showinfo("Message", "You clicked Button 2")


def on_button3_click():
    messagebox.showinfo("Message", "You clicked Button 3")


class MyApplication:
    def __init__(self, master=None):
            ...

            # Configure callbacks
            callbacks = {
                'on_button1_clicked': on_button1_click,
                'on_button2_clicked': on_button2_click,
                'on_button3_clicked': on_button3_click
            }

            builder.connect_callbacks(callbacks)
    ...

Example 2: Using object methods

Example dir: command_properties

File: command_properties2.py

    ...

class MyApplication:
    def __init__(self, master=None):
            ...

            # Connect method callbacks
            builder.connect_callbacks(self)

        # define the method callbacks
        def on_button1_clicked(self):
            messagebox.showinfo('Message', 'You clicked Button 1')

        def on_button2_clicked(self):
            messagebox.showinfo('Message', 'You clicked Button 2')

        def on_button3_clicked(self):
            messagebox.showinfo('Message', 'You clicked Button 3')
    ...
Clone this wiki locally