1. tk command binding

1.1. Overview

In Tkinter, command binding allows you to connect a widget (such as a button or slider) to a Python function. When the user interacts with the widget (e.g., clicks a button), the linked function is executed.

This is essential for making your GUI interactive.

1.2. Basic Syntax

The general syntax for command binding is:

widget = tk.Widget(parent, command=function_name)

Explanation:

  • The command option is used to bind a function to the widget.

  • Do NOT use parentheses () after the function name.

  • Writing command=my_function() will execute the function immediately.


1.3. Example 1: Button Command Binding

import tkinter as tk

def say_hello():
    print("Hello!")

root = tk.Tk()

button = tk.Button(root, text="Click Me", command=say_hello)
button.pack()

root.mainloop()

Explanation:

  • When the button is clicked, say_hello runs.

  • The function is passed as a reference, not called immediately.


1.4. Example 2: Using Lambda for Arguments

Sometimes you need to pass arguments to a function.

import tkinter as tk

def greet(name):
    print("Hello", name)

root = tk.Tk()

button = tk.Button(root, text="Click",
                   command=lambda: greet("Alice"))
button.pack()

root.mainloop()

Explanation:

  • lambda creates an anonymous function.

  • This allows passing arguments safely.


1.5. Example 3: Using Tkinter Variables (DoubleVar)

Command bindings often work with Tkinter variables like DoubleVar.

import tkinter as tk

root = tk.Tk()

value = tk.DoubleVar()

def show_value():
    print(value.get())

scale = tk.Scale(root, from_=0, to=10,
                 resolution=0.1,
                 variable=value)
scale.pack()

button = tk.Button(root, text="Print Value",
                   command=show_value)
button.pack()

root.mainloop()

Explanation:

  • DoubleVar() stores a floating-point number.

  • The scale updates value automatically.

  • The button prints the current value using .get().


1.6. Common Widgets Supporting command

The following widgets support the command option:

  • Button

  • Checkbutton

  • Radiobutton

  • Scale

  • Menu items

Example:

checkbox = tk.Checkbutton(root, text="Accept",
                          command=my_function)

1.7. command vs bind()

Tkinter provides two ways to handle events:

  1. command (simple)

  2. bind() (advanced)


1.8. Using command

  • Easy to use

  • No event object

  • Works for common actions

tk.Button(root, command=my_function)

1.8.1. Using bind()

  • More control (keyboard, mouse events)

  • Provides event information

def on_click(event):
    print("Mouse clicked at", event.x, event.y)

widget.bind("<Button-1>", on_click)

1.9. Key Differences

  • command is simpler, used for basic widget actions.

  • bind() is more powerful, used for detailed event handling.

1.10. Summary

  • command links a widget to a function.

  • Do not call the function directly (no parentheses).

  • Use lambda to pass arguments.

  • Combine with Tkinter variables (DoubleVar, IntVar) for dynamic GUIs.

  • Use bind() for advanced event handling.