How to Create Button in Python TKinter

In this lesson we want to learn How to Create Button in Python TKinter, A button in Tkinter is a widget that is used to interact with the user by performing a specific action when clicked. The Tkinter Button widget can be created by calling the Button() function, which takes one or more options as arguments. Some of the common options that can be passed to the Button widget are:

  • text: The text to be displayed on the button.
  • font: The font to be used for the text.
  • fg: The color of the text.
  • bg: The background color of the button.
  • image: An image to be displayed on the button.
  • command: The function to be called when the button is clicked.

Here is an example of creating a simple button in Tkinter:

 

In the above example, we first import the tkinter module, create a Tk object (root), define a function button_clicked that will be called when button is clicked. Then we create a button widget by calling the Button() function and passing the root window, text options, font options and command which is function that will be called when button is clicked. The pack() method is used to place the button on the screen and the mainloop() method is called to start the event loop and display the button on the screen.

 

 

Run the complete code and this will be the result

How to Create Button in Python TKinter
How to Create Button in Python TKinter

 

 

You can also use grid or place layout management in tkinter

 

In the above example, grid layout is used to place the button on the screen and place layout is used to place the button on the specific position.

 

In Tkinter, there are several types of buttons available:

  • Button: The basic button that can be used to trigger an action or event.
  • Checkbutton: A button that can be toggled on or off.
  • Radiobutton: A button that can be used in a group, where only one button can be selected at a time.
  • Menubutton: A button that opens a menu when clicked.
  • Message: A button that displays text or an image.
  • Spinbox: A button that displays a value and allows the user to increment or decrement the value using up and down arrows.
  • Scale: A button that displays a value and allows the user to select a value using a slider.
  • Listbox: A button that displays a list of items and allows the user to select one or more items.

 

 

Here is an example of creating a Tkinter button that changes the text of a label when clicked:

 

In this example, we import the Tkinter module and create a function change_label_text that changes the text of the label when called. Then we create a root window and a label widget with text “Welcome to Tkinter” in it. Then we create a button widget with text “Click Me!”, and we set the command attribute to the function change_label_text so that when the button is clicked, the change_label_text function will be called. Finally, we use the pack() method to place the label and button widgets in the root window and start the main event loop using root.mainloop().

 

 

 

Run the complete code and this will be the result 

How to Create Button in Python TKinter
How to Create Button in Python TKinter

 

 

Learn More on TKinter

Leave a Comment