Tkinter: Python GUI Programming
What is Tkinter?
• Tkinter is Python's standard GUI (Graphical User Interface) package.
• It is a wrapper around the Tcl/Tk GUI toolkit.
• Comes pre-installed with Python → no need for external installation
Basic Tkinter Structure
import tkinter as tk
# Create the main application window
root = [Link]()
# Set the window title
[Link]("My First GUI")
# Set the window size
[Link]("300x200")
# Add widgets here
# Start the GUI event loop
[Link]()
Widgets (GUI Elements)
Label – Display text Button – Trigger actions
label = [Link](root, text="Hello, Tkinter!") def on_click():
[Link]() print("Button clicked!")
button = [Link](root, text="Click Me", command=on_click)
Entry – Input field for one-line text Text – Multi-line text input
entry = [Link](root) text_box = [Link](root, height=5, width=30)
[Link]() text_box.pack()
Checkbutton – Checkbox (on/off) Radiobutton – Select one from many
var = [Link]() var = [Link]()
check = [Link](root, text="Check radio1 = [Link](root, text="Option A", variable=var,
me", variable=var) value="A")
[Link]() radio2 = [Link](root, text="Option B", variable=var,
value="B")
[Link]()
[Link]()
Listbox – Select from list Scale – Slider
listbox = [Link](root) scale = [Link](root, from_=0, to=100, orient=[Link])
[Link](1, "Python") [Link]()
[Link](2, "Java")
[Link](3, "C++")
[Link]()
Layout Managers .grid() – Place widgets in rows and columns
.pack() – Simplest, stack widgets [Link](row=0, column=0)
vertically/horizontally [Link](row=0, column=1)
[Link]()
.place() – Absolute positioning Widget Options
[Link](x=50, y=100) • text: sets label or button text
• bg: background color
• fg: foreground (text) color
• font: font type and size (e.g., ("Arial", 12, "bold"))
• width, height: size
• command: function called on click
[Link](root, text="OK", bg="green", fg="white",
font=("Helvetica", 12)).pack()
Event Handling
def say_hello():
print("Hello!")
button = [Link](root, text="Say Hello", command=say_hello)
Binding events:
def on_key(event):
print(f"Key pressed: {[Link]}")
[Link]("<Key>", on_key)
File Dialogs:
from tkinter import filedialog
def open_file():
file_path = [Link]()
print(file_path)
[Link](root, text="Open File", command=open_file).pack()
Message Boxes
from tkinter import messagebox
[Link]("Title", "This is an information message.")
Adding Images
from tkinter import PhotoImage
img = PhotoImage(file="[Link]")
label = [Link](root, image=img)
[Link]()
Simple Example – Login GUI
import tkinter as tk
def login():
user = [Link]()
pwd = [Link]()
print(f"Username: {user}, Password: {pwd}")
root = [Link]()
[Link]("Login")
[Link]("300x150")
[Link](root, text="Username").pack()
username = [Link](root)
[Link]()
[Link](root, text="Password").pack()
password = [Link](root, show="*")
[Link]()
[Link](root, text="Login", command=login).pack()
[Link]()
Note:
• Always end with [Link]() to keep the window running.
• Use [Link]() to group widgets.
• Avoid mixing pack() and grid() in the same container.
• Use .get() to read values from Entry or Text.
• Start small and build incrementally.