Tkinter is the standard Python library for creating graphical user interfaces (GUIs). It allows you to build desktop-based applications with ease. Below is an overview of Tkinter's basic concepts, widgets, and geometry management.
Import Tkinter module: First, you need to import the Tkinter module.
Create the main application window: This is the window where your widgets will be placed.
Add widgets: Add GUI elements such as buttons, labels, and entry fields to the window.
Start the main loop: The mainloop() method is called to run the application and keep the window open.
from tkinter import *
# Create the main application window
root = Tk()
# Start the main event loop
root.mainloop()
Tkinter provides various widgets that you can use to build the graphical interface. Below is a list of commonly used widgets:
| Widget | Description |
|---|---|
| Button | Adds a clickable button. |
| Canvas | Used for drawing shapes and other graphics. |
| Checkbutton | Displays a checkbox. |
| Entry | Provides a single-line text input field. |
| Frame | Acts as a container for organizing other widgets. |
| Label | Displays text or images. |
| ListBox | Shows a list of items from which the user can select. |
| Menubutton | Displays a menu. |
| Menu | Adds menu items to the GUI. |
| Message | Displays multi-line text messages. |
| Radiobutton | Allows the user to select only one option from multiple choices. |
| Scale | Adds a slider to the interface. |
| Scrollbar | Adds a scrollable window for viewing content. |
| Text | Displays a multi-line text field for editing. |
| Toplevel | Creates a new window on top of the main application window. |
| Spinbox | Allows the user to select from a set of values. |
| PanedWindow | Contains panes (sections) where widgets can be placed. |
| LabelFrame | A container that can hold other widgets within a labeled frame. |
| MessageBox | Displays a message box. |
Tkinter provides three geometry managers for organizing widgets within the window.
The pack() method organizes widgets in blocks. It is simple but not suitable for complex layouts.
widget.pack(options)
• expand: Expands the widget to fill available space.
• fill: Specifies whether to fill the widget horizontally (X) or vertically (Y).
• side: Specifies which side of the parent window the widget should be placed.
The grid() method organizes widgets in a tabular form by specifying rows and columns.
widget.grid(options)
• row: Specifies the row where the widget should be placed.
• column: Specifies the column where the widget should be placed.
• columnspan: Specifies how many columns the widget should span.
• rowspan: Specifies how many rows the widget should span.
• sticky: Specifies the position of the widget inside the grid cell (N, E, S, W, etc.).
The place() method organizes widgets by specifying their exact x and y coordinates.
widget.place(options)
• x, y: Specifies the x and y coordinates.
• relx, rely: Specifies relative position as a fraction of the parent's size (values between 0.0 and 1.0).
• height, width: Specifies the height and width in pixels.
from tkinter import *
# Create the main application window
root = Tk()
# Add a label using pack() method
label1 = Label(root, text="Label with pack")
label1.pack()
# Add a button using grid() method
button1 = Button(root, text="Button with grid")
button1.grid(row=0, column=0)
# Add another button using place() method
button2 = Button(root, text="Button with place")
button2.place(x=50, y=100)
# Start the main event loop
root.mainloop()
• Basic knowledge of Python is recommended.
• This tutorial is designed for beginners and professionals who want to learn how to create desktop applications using Python Tkinter.
By following this tutorial, you can develop real-world desktop applications using Python and Tkinter with ease.