0% found this document useful (0 votes)
2 views

Tkinter Python

Tkinter is Python's standard GUI package that wraps the Tcl/Tk toolkit and comes pre-installed with Python. The document provides a basic structure for creating a Tkinter application, including examples of various widgets and layout managers, as well as event handling and file dialogs. It concludes with a simple login GUI example and important notes on best practices for using Tkinter.

Uploaded by

ha3021413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tkinter Python

Tkinter is Python's standard GUI package that wraps the Tcl/Tk toolkit and comes pre-installed with Python. The document provides a basic structure for creating a Tkinter application, including examples of various widgets and layout managers, as well as event handling and file dialogs. It concludes with a simple login GUI example and important notes on best practices for using Tkinter.

Uploaded by

ha3021413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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 = tk.Tk()

# Set the window title

root.title("My First GUI")

# Set the window size

root.geometry("300x200")

# Add widgets here

# Start the GUI event loop

root.mainloop()

Widgets (GUI Elements)

Label – Display text Button – Trigger actions


label = tk.Label(root, text="Hello, Tkinter!") def on_click():
label.pack() print("Button clicked!")
button = tk.Button(root, text="Click Me", command=on_click)
Entry – Input field for one-line text Text – Multi-line text input
entry = tk.Entry(root) text_box = tk.Text(root, height=5, width=30)
entry.pack() text_box.pack()
Checkbutton – Checkbox (on/off) Radiobutton – Select one from many
var = tk.IntVar() var = tk.StringVar()
check = tk.Checkbutton(root, text="Check radio1 = tk.Radiobutton(root, text="Option A", variable=var,
me", variable=var) value="A")
check.pack() radio2 = tk.Radiobutton(root, text="Option B", variable=var,
value="B")
radio1.pack()
radio2.pack()
Listbox – Select from list Scale – Slider
listbox = tk.Listbox(root) scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)
listbox.insert(1, "Python") scale.pack()
listbox.insert(2, "Java")
listbox.insert(3, "C++")
listbox.pack()
Layout Managers .grid() – Place widgets in rows and columns
.pack() – Simplest, stack widgets label.grid(row=0, column=0)
vertically/horizontally entry.grid(row=0, column=1)
label.pack()
.place() – Absolute positioning Widget Options
label.place(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
tk.Button(root, text="OK", bg="green", fg="white",
font=("Helvetica", 12)).pack()

Event Handling

def say_hello():

print("Hello!")

button = tk.Button(root, text="Say Hello", command=say_hello)

Binding events:

def on_key(event):

print(f"Key pressed: {event.char}")

root.bind("<Key>", on_key)

File Dialogs:

from tkinter import filedialog

def open_file():

file_path = filedialog.askopenfilename()

print(file_path)
tk.Button(root, text="Open File", command=open_file).pack()

Message Boxes

from tkinter import messagebox

messagebox.showinfo("Title", "This is an information message.")

Adding Images

from tkinter import PhotoImage

img = PhotoImage(file="image.png")

label = tk.Label(root, image=img)

label.pack()

Simple Example – Login GUI

import tkinter as tk

def login():

user = username.get()

pwd = password.get()

print(f"Username: {user}, Password: {pwd}")

root = tk.Tk()

root.title("Login")

root.geometry("300x150")

tk.Label(root, text="Username").pack()

username = tk.Entry(root)

username.pack()

tk.Label(root, text="Password").pack()

password = tk.Entry(root, show="*")

password.pack()

tk.Button(root, text="Login", command=login).pack()

root.mainloop()
Note:

• Always end with root.mainloop() to keep the window running.

• Use tk.Frame() 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.

You might also like