Tkinter Widget command parameter
Many Widgets can see the command parameter within the constructor method, for example, function button (Button), value scrollbar (Scale), etc.. In fact, this is the concept of a Widget’s event binding, when the button event occurs, when the value of the value scroll bar changes …… can be designed callback function by command=callback, this callback function is the event handler.
Tkinter Widget command parameter Example
When a function button is clicked or a checkbox is selected, the action performed at the bottom of the window is made using the command parameter in the widget control’s constructor.
from tkinter import *
def pythonClicked(): # Python Checkbox Event Handler
if varPython.get():
lab.config(text="Select Python")
else:
lab.config(text="Unselect Python")
def javaClicked(): # Java Checkbox Event Handler
if varJava.get():
lab.config(text="Select Java")
else:
lab.config(text="Unselect Java")
def buttonClicked():
lab.config(text="Button clicked")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
btn = Button(root,text="Click Me",command=buttonClicked)
btn.pack(anchor=W)
varPython = BooleanVar()
cbnPython = Checkbutton(root,text="Python",variable=varPython,
command=pythonClicked)
cbnPython.pack(anchor=W)
varJava = BooleanVar()
cbnJava = Checkbutton(root,text="Java",variable=varJava,
command=javaClicked)
cbnJava.pack(anchor=W)
lab = Label(root,bg="yellow",fg="blue",
height=2,width=12,
font="Times 16 bold")
lab.pack()
root.mainloop()
Output: