Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
tkinter
is the standard Python interface to the Tk GUI (Graphical User Interface) toolkit. It provides a simple way to create windows, dialogs, buttons, and other GUI elements for desktop applications. With tkinter
, you can develop feature-rich and cross-platform desktop applications that run on Windows, macOS, and Linux.
tkinter
:Importing the Library:
Most commonly, tkinter
is imported with the following line:
import tkinter as tk
Main Application Window:
Every tkinter
application starts with a main window. This is the primary container that houses other GUI elements.
root = tk.Tk()
Widgets:
tkinter
offers a variety of widgets to build your GUI:
Basic Widgets:
Label
: Displays text or an image.Button
: Triggers a function when clicked.Entry
: Allows the user to enter a single line of text.Text
: Used for multiline text fields.Checkbutton
: Provides a checkbox.Radiobutton
: Allows the selection of a single option from a set.Advanced Widgets:
Listbox
: Displays a list of options.Scrollbar
: Adds scrolling capability to other widgets.Canvas
: Draws shapes, like lines, ovals, and polygons.Menu
: Provides menus and submenus.Geometry Managers:
pack()
: Packs widgets in order, one after the other.grid()
: Arranges widgets in a grid.place()
: Allows you to specify exact coordinates for widgets.Events and Binds: In GUI programming, events are driven by actions such as button clicks, mouse movement, and keyboard input. You can bind functions to respond to such events.
def on_button_click(): print("Button clicked!") button = tk.Button(root, text="Click Me", command=on_button_click) button.pack()
Dialogs:
tkinter
provides a set of dialog windows for common tasks like file selection, color selection, and simple alerts:
tkinter.messagebox
: Provides methods for displaying message boxes.tkinter.filedialog
: Functions like askopenfilename()
and asksaveasfilename()
allow for file selection.Styling and Themes:
While base tkinter
offers basic styling options, the ttk
(themed tkinter
) module provides access to the Tk themed widget set, offering more advanced styling and themes.
Canvas and Graphics:
The Canvas
widget provides the capability to draw graphics and create custom widgets like charts and graphs.
Ending the Main Loop: Once you've set up your GUI layout and functions, you run the application's main event loop using:
root.mainloop()
tkinter
Application:Here's a simple application that has a label, an entry field, and a button. When the button is clicked, it updates the label with the text from the entry field:
import tkinter as tk def update_label(): label.config(text="Hello, " + name_entry.get() + "!") root = tk.Tk() root.title("tkinter Sample App") label = tk.Label(root, text="Enter your name:") label.pack(pady=10) name_entry = tk.Entry(root) name_entry.pack(pady=10) submit_btn = tk.Button(root, text="Submit", command=update_label) submit_btn.pack(pady=10) root.mainloop()
tkinter
is a vast library, and this overview just scratches the surface. However, with this foundation, you can explore more complex GUI designs and applications.