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

Desktop Programming in Python With Tkinter

Uploaded by

Khalil Hafiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Desktop Programming in Python With Tkinter

Uploaded by

Khalil Hafiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tkinter is a built-in Python library used to create graphical user interfaces (GUIs) for desktop

applications. It provides a variety of UI elements (widgets) and layout management options.

Why use tkinter?


Built-in: No need for external installations, as it comes with Python.
Cross-platform: Works on Windows, macOS, and Linux.
Simple API: Easy to learn and use for creating simple and complex GUIs.

UI elements (widgets) in tkinter


Widgets are the building blocks of a Tkinter interface. Below are some commonly used widgets:

Label: Displays text or images.


Button: Triggers actions when clicked.
Entry: Allows text input.
Text: Multi-line text input.
Checkbutton: Checkbox for selecting options.
Radiobutton: Radio buttons for selecting one option from a group.
Listbox: Displays a list of selectable items.
Canvas: For drawing shapes, lines, and images.

Frames in tkinter
A Frame is a container that groups related widgets together. It helps organize the layout of an
application and makes it easier to manage and style widgets.

Why use frames?

Frames allow you to group related widgets together.


You can reuse frames in different parts of your application.
Makes the code more modular and easier to maintain.

Example: creating and using frames

import tkinter as tk
root = tk.Tk()
root.title("Frame Example")

# Create a frame
frame = tk.Frame(root, bg="lightblue", padx=10, pady=10)
frame.pack(padx=20, pady=20)

# Add widgets to the frame


label = tk.Label(frame, text="This is a label inside a frame")
label.pack()

button = tk.Button(frame, text="Click Me")


button.pack()

root.mainloop()

Layout management: grid vs. pack


Tkinter provides different ways to arrange widgets in a window. The two most commonly used
methods are pack and grid.

Pack geometry manager

Description: Places widgets in a container in a top-to-bottom or left-to-right order.


Best for: Simple layouts or when you want widgets stacked in a specific order.

import tkinter as tk

root = tk.Tk()
label1 = tk.Label(root, text="Label 1")
label1.pack(side="top")

label2 = tk.Label(root, text="Label 2")


label2.pack(side="left")

root.mainloop()

Grid geometry manager

Description: Places widgets in a grid-like structure with rows and columns.


Best for: More complex layouts where precise control over placement is needed.

Example: using grid


import tkinter as tk

root = tk.Tk()
label1 = tk.Label(root, text="Label 1")
label1.grid(row=0, column=0)

label2 = tk.Label(root, text="Label 2")


label2.grid(row=0, column=1)

label3 = tk.Label(root, text="Label 3")


label3.grid(row=1, column=0)

root.mainloop()

Creating a calculator desktop app


1. Create a basic window:

import tkinter as tk

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

2. Add an Entry widget for input:

entry = tk.Entry(root, width=20, font=("Arial", 16), justify="right")


entry.grid(row=0, column=0, columnspan=4)

3. Add Buttons for numbers and operations:

def button_click(value):
entry.insert(tk.END, value)

# A list of tuples, in each tuple the first item


# is the button character e.g. '7', '/', '='
# the second item is the button's row and the last is the column
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
]

for (text, row, col) in buttons:


button_command = calculate if text == '=' else lambda t=text:
button_click(t)
button = tk.Button(root, text=text, command=button_command)
button.grid(row=row, column=col, padx=5, pady=5, sticky="nsew")

4. Add functionality for calculations:

def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(0, result)
except Exception as e:
entry.delete(0, tk.END)
entry.insert(0, "Error")

# Update '=' button to call calculate


equal_button = tk.Button(root, text='=', command=calculate)
equal_button.grid(row=4, column=2)

5. Run the main event loop:

root.mainloop()

Other Python desktop programming libraries


1. PyQt / PySide
Features:
Extensive set of widgets and tools.
Advanced layout management and custom widget creation.
Cross-platform compatibility with native look and feel.
Use case:
Ideal for building complex, feature-rich desktop applications with cross-platform
support.
2. Kivy
Features:
Supports multi-touch and gestures.
Works on desktop and mobile platforms (Windows, macOS, Linux, Android,
iOS).
Highly customizable widgets and layouts.
Use Case:
Best for developing modern, touch-friendly, and cross-platform apps.
3. CustomTkinter
Features:
Modern, customizable widgets with a sleek appearance.
Built on top of Tkinter but allows for better theming (light, dark, system).
Simplifies styling and widget customization.
Use Case:
Great for developers who want the simplicity of Tkinter but with a modern UI look.
Example Usage:

import customtkinter as ctk

app = ctk.CTk()
app.title("CustomTkinter Example")

button = ctk.CTkButton(app, text="Click Me")


button.pack(pady=20)

app.mainloop()

4. Flet
Features:
Allows building interactive desktop, web, and mobile apps using Python.
Built on Flutter, giving access to rich UI components.
Hot-reload for rapid development.
Cross-platform support for Windows, macOS, Linux, and the web.
Use Case:
Ideal for developers wanting to create multi-platform applications with a consistent
look and feel using Python.
Example Usage:

import flet as ft
def main(page: ft.Page):
page.title = "Flet Example"
page.add(ft.Text("Hello, Flet!"))

ft.app(target=main)

You might also like