Here we are providing basic tutorial in Tkinter to bind functions to a widget using events.

Consider this code

from tkinter import *

root = Tk()

def printName(event):
    print("Hello, Welcome to codeshot")

button_1 = Button(root, text="Print my name")
button_1.bind("<Button-1>", printName)
button_1.pack()

root.mainloop()

In above code, first we created a function printName that just prints something on screen. Then a button button_1 is created in the code. An event parameter is being passed to the printName function. A new function bind is called for button_1 with the parameter event name i.e Button-1 in above case and name of the function that must be called when this event occurs i.e printName in above case.

Output

output


On pressing the button, output will appear on command window like this -


output