Here we are providing basic tutorial in Tkinter to bind function with a widget.

Consider this code

from tkinter import *

root = Tk()

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

button_1 = Button(root, text="Print my name", command=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. A new parameter command is present in the function Button that is used to bind the function printName to the widget button_1.

Output

output


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


output