How to Create Button in Python TKinter

In this article we want to learn How to Create Button in Python TKinter, so first of all let’s talk about TKinter Button.

 

 

 

What is Python TKiner Button?

Button in Tkinter is a widget that is used to interact with users by performing specific actions . 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.

 

 

 

This is an example of creating simple button in Tkinter:

In the above example, we have imported import tkinter module, after that we created a Tk object (root), also we have defined a function button_clicked that will be called when button is clicked. Then we created a button widget by calling Button() function and passing the root window, text options, font options and command which is function that will be called when button is clicked. 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.

 

 

Types of Buttons in TKinter

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.

 

 

This is is an example of creating a Tkinter button that changes the text of a label, when we click the tkinter button:

 

 

 

Run the complete code and this will be the result 

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

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×