Mastering Tkinter: A Comprehensive Guide to GUI Widgets in Python
Written on
Chapter 1: Introduction to Tkinter Widgets
In our previous discussion, we explored the foundational concepts of Tkinter. If you missed it, you can find that article linked here. This piece will primarily focus on the various widgets available in Tkinter. Widgets serve as the interactive elements through which users engage with the operating system, including buttons, labels, menus, and more.
Creating a Simple Button
In this section, we will develop a basic button that, when clicked, will close the main window.
Example Code:
# Importing the Tkinter library
from tkinter import *
# Creating an object to initialize the root window
root = Tk()
# Setting the window size for Tkinter
root.geometry('200x100')
# Creating a button that closes the window upon being clicked
button = Button(root, text='Click me!', bd='5', command=root.destroy)
# Using the pack method to position the button
button.pack(side='top')
root.mainloop()
Output: A simple button.
Creating a Button with an Image
In this example, we'll enhance the button's appearance by adding an image alongside its text. This button will not have the close functionality.
Example Code:
# Importing necessary libraries
from tkinter import *
from tkinter.ttk import *
# Creating an object to initialize the root window
root = Tk()
# Adding a label to the window
Label(root, text='This is a Button with an image', font=('Ariel', 10)).pack(side=LEFT, pady=10)
# Specifying the path to the image
photo = PhotoImage(file=r"index.png")
# Resizing the image
photoimage = photo.subsample(3, 3)
# Creating a button with an image
Button(root, text='Click Me!', image=photoimage, compound=LEFT).pack(side=TOP)
mainloop()
Output: Button featuring an image.
Creating Labels
In this section, we will create labels within a frame to display both text and images. When creating labels, parameters such as height, width, and font must be specified.
Example Code:
# Importing the library
from tkinter import *
# Creating an object to initialize the root window
root = Tk()
# Setting the window size
root.geometry("450x300")
# Creating a label for the user name
user_name = Label(root, text="Your Name").place(x=40, y=60)
# Creating a submit button
submit_button = Button(root, text="Submit").place(x=40, y=100)
# Creating a text input area
input_area = Entry(root, width=30).place(x=110, y=60)
root.mainloop()
Output: Label example.
Creating Radio Buttons
Radio buttons allow users to select one option from a set. In this example, we will create a radio button interface.
Example Code:
# Importing the library
from tkinter import *
from tkinter.ttk import *
# Creating an object to initialize the root window
root = Tk()
root.geometry("175x100")
# Variable to store the selected radio button's value
str_var = StringVar(root, "1")
# Creating radio buttons using a dictionary
values = {"Radio Button": "1"}
# Looping through the dictionary to create radio buttons
for (word, value) in values.items():
Radiobutton(root, text=word, variable=str_var, value=value).pack(side=LEFT, ipady=6)
root.mainloop()
Output: Radio button.
Creating Check Buttons
Check buttons are simple elements that allow users to select multiple options. Here’s how to create them.
Example Code:
# Importing the library
from tkinter import *
# Creating an object to initialize the root window
root = Tk()
root.geometry("400x150")
# Adding a label to the window
label_text = Label(root, text='This window is for check buttons', font="40")
label_text.pack()
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
# Creating check buttons
Button1 = Checkbutton(root, text="Happy", variable=Checkbutton1, onvalue=1, offvalue=0, height=2, width=10)
Button2 = Checkbutton(root, text="Neutral", variable=Checkbutton2, onvalue=1, offvalue=0, height=2, width=10)
Button3 = Checkbutton(root, text="Sad", variable=Checkbutton3, onvalue=1, offvalue=0, height=2, width=10)
Button1.pack()
Button2.pack()
Button3.pack()
root.mainloop()
Output: Check list buttons.
Restricting the Size of the Window
This widget restricts the window size, ensuring it remains fixed according to specified dimensions.
Example Code:
# Importing the libraries
from tkinter import *
from tkinter.ttk import *
# Creating an object to initialize the root window
root = Tk()
root.title('Window Size Restrict')
root.geometry('250x100')
Label(root, text='This window size is fixed').pack(side=TOP, pady=10)
# Restricting window resizing
root.resizable(0, 0)
root.mainloop()
Output: Window size restriction.
Creating a Spinbox
A spinbox allows users to select a value from a defined range using up and down arrows.
Example Code:
# Importing the libraries
from tkinter import *
# Creating an object to initialize the root window
root = Tk()
root.geometry("200x100")
# Adding a label to the window
label_text = Label(root, text='Spin Box', font="50")
label_text.pack()
# Creating a spinbox for selection
spin_box = Spinbox(root, from_=0, to=5)
spin_box.pack()
root.mainloop()
Output: Spin box.
I hope you found this article helpful. Feel free to connect with me on LinkedIn and Twitter.
Recommended Articles
- 15 Most Usable NumPy Methods with Python
- NumPy: Linear Algebra on Images
- Exception Handling Concepts in Python
- Pandas: Dealing with Categorical Data
- Hyper-parameters: RandomSearchCV and GridSearchCV in Machine Learning
- Fully Explained Linear Regression with Python
- Fully Explained Logistic Regression with Python
- Data Distribution using Numpy with Python
- 40 Most Usable Methods in Python
- 20 Most Usable Pandas Shortcut Methods in Python
Chapter 2: Video Tutorials on Tkinter
To further enhance your understanding of Tkinter, here are two informative video tutorials.
The first video, "The Ultimate Introduction to Modern GUIs in Python [with Tkinter]," provides a comprehensive overview of creating graphical user interfaces using Tkinter.
The second video, "How to Create a GUI in Python using Tkinter," walks you through step-by-step instructions on building GUIs with this powerful library.