Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

4.2 GUI Programming with Tkinter

Lesson 26 of 32 in the free Data Visualisation and Analytics notes on Siksha Sarovar, written by Rohit Jangra.

GUI Programming: Creating User Interfaces with Tkinter

1. Introduction to GUI (Graphical User Interface)

A GUI allows users to interact with a program using visual elements like windows, buttons, and text boxes, rather than just text commands. Tkinter is Python's standard GUI library. It is lightweight and included with Python by default (no pip install needed).

Basic Structure of a Tkinter Program:

  1. Import: import tkinter as tk
  2. Create Main Window: root = tk.Tk()
  3. Configure Window: root.title("My App"), root.geometry("400x300")
  4. Add Widgets: Labels, Buttons, Entry fields, etc.
  5. Enter Main Loop: root.mainloop() (Keeps the window open and listening for events).

2. Tkinter Widgets

Widgets are the building blocks of a GUI.

WidgetDescriptionSyntaxCommon Options
LabelDisplays text or images (static)tk.Label(root, text="Hello")font, fg, bg, anchor
ButtonClickable button to trigger actionstk.Button(root, text="Click", command=func)state, width, relief
EntrySingle-line text inputtk.Entry(root)show (for passwords), width
TextMulti-line text inputtk.Text(root, height=5)wrap, state
FrameContainer to group widgetstk.Frame(root, bd=2, relief="groove")padx, pady, bg
CanvasDrawing area for shapes/graphicstk.Canvas(root, width=400, height=300)bg, shapes via methods
ScrollbarScroll through contenttk.Scrollbar(root, orient="vertical")command (links to widget)

3. Layout Managers (Geometry Management)

You must use a layout manager to place widgets in the window.

Rule: You CANNOT mix pack() and grid() in the same parent container. Choose one per Frame/Window.
ManagerHow It WorksBest Use CaseKey Options
.pack()Stacks widgets in blocks (top/bottom/left/right)Simple vertical/horizontal layoutsside, fill, expand, padx/pady
.grid()Places widgets in 2D table (rows and columns)Forms, calculators, complex layoutsrow, column, columnspan, sticky
.place()Absolute X, Y pixel coordinatesExact positioning (NOT responsive)x, y, relx, rely

4. Event Handling

GUI programming is event-driven — the program waits for user actions (clicks, key presses) and responds.

Methods to Handle Events:

MethodDescriptionExample
command=Simple callback for button clicksButton(root, command=my_func)
.bind()Bind any event to any widgetwidget.bind("<Button-1>", callback)
after()Schedule a function to run after delayroot.after(1000, update_clock)

Common Events:

  • <Button-1> — Left mouse click
  • <Return> — Enter key pressed
  • <Key> — Any key pressed
  • <FocusIn> / <FocusOut> — Widget gains/loses focus

5. Code Example (Grid Layout)

import tkinter as tk

root = tk.Tk()
root.title("Login")

# Row 0
tk.Label(root, text="Username").grid(row=0, column=0, padx=10, pady=5)
tk.Entry(root).grid(row=0, column=1, padx=10, pady=5)

# Row 1
tk.Label(root, text="Password").grid(row=1, column=0, padx=10, pady=5)
tk.Entry(root, show="*").grid(row=1, column=1, padx=10, pady=5)

# Row 2
tk.Button(root, text="Login", command=lambda: print("Logged in!")).grid(row=2, column=0, columnspan=2, pady=10)

root.mainloop()