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:
1 2 3 4 5 6 7 8 9 10 |
import tkinter as tk root = tk.Tk() def button_clicked(): print("Button clicked") button = tk.Button(root, text="Click me", font=("Arial", 20), command=button_clicked) button.pack() root.mainloop() |
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
You can also use grid or place layout management in tkinter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import tkinter as tk root = tk.Tk() def button_clicked(): print("Button clicked") button = tk.Button(root, text="Click me", font=("Arial", 20), command=button_clicked) button.grid(row=0, column=0) root.mainloop() # or root = tk.Tk() def button_clicked(): print("Button clicked") button = tk.Button(root, text="Click me", font=("Arial", 20), command=button_clicked) button.place(x=20, y=20) root.mainloop() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import tkinter as tk def change_label_text(): label.config(text="Welcome to codeloop.org") root = tk.Tk() label = tk.Label(root, text="Welcome to Tkinter") label.pack() button = tk.Button(root, text="Click Me", command=change_label_text) button.pack() root.mainloop() |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email