Tkinter Button command Option

Tkinter Button command option sets the function or method to be called when the button is clicked.

To set a function for execution on button click, define a Python function, and assign this function name to the command option of Button.

In this tutorial, we will learn how to use command option of Button() class with examples.

Example 1 – Tkinter Button Call Function on Button Click

In the following program, we will use command option of Tkinter Button, to call a function when this button is clicked.

example.py – Python Program

</>
Copy
import tkinter

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

def submitFunction() :
    print('Submit button is clicked.')

button_submit = tkinter.Button(window_main, text ="Submit", command=submitFunction)
button_submit.config(width=20, height=2)

button_submit.pack()
window_main.mainloop()

Output

Tkinter Button Command Option - Call Function when Button is Clicked

When user clicks the Submit button, submitFunction() is called. In this example, we are just printing a string to standard console.

Submit button is clicked.

Conclusion

In this Python Tutorial, we learned about Tkinter Button bg option, to change background color, with the help of example Python programs.