Python Tkinter - Introduction


Django

What is Tkinter?

Tkinter is the standard GUI (Graphical User Interface) library for Python.provides tools to create windows, buttons, textboxes, labels, and other graphical elements that help users interact with your application visually. It is built on top of the Tcl/Tk GUI toolkit, and it comes bundled with Python, so you don't need to install it separately.

Basic App Structure

Example.py
import tkinter as tk

# Create the main window
root = tk.Tk()
root.title("Simple Tkinter App")
root.geometry("300x200")

# Create a label
label = tk.Label(root, text="Hello, World!")
label.pack(pady=20)

# Run the application
root.mainloop()

Output

This is the output of the code above, showing the basic window layout and displaying the text 'Hello World'.

Python Tkinter Introduction Output

Significance of Tkinter

  1. Built-in GUI library in Python (no need for installation).
  2. Cross-platform: Works on Windows, macOS, and Linux.
  3. Simple and beginner-friendly: Great for learning GUI programming.
  4. Wide range of widgets: Buttons, labels, entry boxes, menus, and more.
  5. Customizable: You can change fonts, colors, and layout easily.
  6. Event-driven: Responds to user inputs like mouse clicks or key presses.

Why Do We Use Tkinter?

  • Build desktop GUI applications using Python.
  • Create user-friendly interfaces for scripts and tools.
  • Build apps like:
    1. Calculator
    2. Form submission apps
    3. Notepad
    4. Games with buttons and labels
    5. Inventory or student management systems

Tkinter is perfect for small to medium desktop apps that don't require advanced GUI features or modern design aesthetics.

Widgets Definition / Purpose
LabelDisplays text or images.
ButtonCreates a clickable button.
EntrySingle-line text input field.
TextMulti-line text input area.
FrameContainer to group widgets.
CheckbuttonCheckbox for on/off (True/False) options.
RadiobuttonSet of buttons where only one can be selected.
ListboxDisplays a list of items for selection.
CanvasWidget for drawing shapes, images, or custom graphics.
ScrollbarAdds scrolling ability to widgets like Text, Listbox, or Canvas.
ScaleSlider for selecting a numerical value.
SpinboxInput box with up/down arrows to increment/decrement values.
MenuMenu bar for creating dropdown menus.
MenubuttonButton that displays a menu when clicked.
PanedWindowContainer with adjustable panes.
LabelFrameFrame with a label on top. Used for grouping widgets.
ToplevelCreates a new top-level window separate from the root.
MessageSimilar to Label, but supports automatic text wrapping.
ComboboxDropdown menu (from ttk module).
TreeviewTable/tree structure for displaying hierarchical data (from ttk).
NotebookTabbed layout (from ttk).
Progressbar Displays a horizontal or vertical progress indicator (from ttk).
SeparatorDraws a horizontal or vertical line to separate widgets (from ttk).
DialogPredefined dialog boxes like message boxes, file dialogs, etc.
MessageboxDisplays informational, warning, error, or question dialogs (tk.messagebox).
OptionMenuDrop-down menu for selecting from a list of options.