Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
In Tkinter, which is the standard GUI toolkit for Python, the term "widget" refers to a visual element or component in the user interface. These components can range from simple ones like labels and buttons to more complex ones like canvases and text boxes.
Each widget in Tkinter corresponds to a class, and you create an instance of that class to place and use the widget in your GUI.
Here are some of the most commonly used widgets in Tkinter:
Tk
: This is the main window or root window widget. Every Tkinter application will have one main window.
Label
: A widget used to display text or images. It's non-interactive, meaning users can't directly interact with it.
Button
: A widget that provides interactivity. It can trigger a function or method when clicked.
Entry
: A widget for single-line text input.
Text
: A widget for multi-line text input, useful for things like a text editor.
Frame
: A container widget, which can hold other widgets. It's primarily used for organization and layout purposes.
Canvas
: A versatile widget that can be used for custom drawing, such as graphics, plots, or even game interfaces.
Checkbutton
: Also known as a checkbox, it allows the user to select or unselect an option.
Radiobutton
: Allows the user to select one of many options.
Scale
: A slider widget that lets users select a value from a specific range.
Scrollbar
: Offers scrolling capabilities when added to other widgets like lists, canvases, or text boxes.
Listbox
: Displays a list of items from which a user can select.
Menu
: Creates dropdown menus in the GUI.
Toplevel
: A separate window, typically used for things like pop-up dialogs.
Message
: Similar to a label but is optimized to display multiline messages.
Spinbox
: Allows the user to select from a given set of values, typically numbers.
PanedWindow
: A container widget that can contain one or more child widgets, with a movable sash between them.
Notebook
(from ttk
): Tabbed widget, allowing for a multi-pane layout.
Combobox
(from ttk
): Dropdown widget that combines a text entry and a dropdown list.
Progressbar
(from ttk
): Displays a progress bar for tasks.
Tkinter also provides many other widgets and many options for customizing the behavior and appearance of these widgets.
Creating an effective GUI requires understanding how to use these widgets and how to organize (layout) them within the main window or within other container widgets. In Tkinter, this organization is achieved using geometry managers like pack()
, grid()
, and place()
.
Tkinter Widgets Explanation:
import tkinter as tk root = tk.Tk() button = tk.Button(root, text="Click me") button.pack() root.mainloop()
List of Tkinter Widgets in Python:
import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello, Tkinter!") entry = tk.Entry(root) button = tk.Button(root, text="Submit") label.pack() entry.pack() button.pack() root.mainloop()
Tkinter Widget Examples for Beginners:
import tkinter as tk def on_button_click(): label.config(text=f"Hello, {entry.get()}!") root = tk.Tk() label = tk.Label(root, text="Enter your name:") entry = tk.Entry(root) button = tk.Button(root, text="Say Hello", command=on_button_click) label.pack() entry.pack() button.pack() root.mainloop()