DigitalSkills&Python_Chapter10
DigitalSkills&Python_Chapter10
Chapter 10
Python Graphical User Interface (GUI):
Introduction to Tkinter Module
Create First
GUI Application
Tkinter
6
Tkinter Label Widget:
PYTHON The Label widget is used to display static text or
GUI images in the GUI:
Example:
import tkinter as tk
window = tk.Tk()
window.title("Image in Label Example")
Create First # Load an image
GUI Application image = tk.PhotoImage(file="python.png")
Tkinter # Create a label with the image
label = tk.Label(window, image=image)
label.pack()
window.mainloop()
15
Tkinter Radiobutton Widget:
PYTHON
The Radiobutton widget allows users to select only
GUI one option from the given ones (mutually exclusive
options):
radio1 = tk.Radiobutton(window, text="Option 1",
value=1, variable=selected_option)
radio2 = tk.Radiobutton(window, text="Option 2",
Create First value=2, variable=selected_option)
GUI Application radio1.pack()
radio2.pack()
Tkinter
window.mainloop()
To get the selected radiobutton value we use:
selected_option.get()
16
Tkinter Scale Widget:
PYTHON
The Scale widget provides a slider that lets users
GUI select a numeric value from a defined range:
Create First
GUI Application
Tkinter
window.mainloop()
23
Tkinter Frame Widget:
PYTHON The Tkinter Frame widget is an invisible container
GUI used to group and organize the widgets in a better
and friendly way.
The Tkinter frame widget makes up a rectangular
region on the screen.
Syntax: W = Frame(master, options)
Create First Common Options:
• bg: Background color of the frame.
GUI Application
• width: Width of the frame.
Tkinter
• height: Height of the frame.
• relief: Border style (e.g., FLAT, RAISED, SUNKEN,
GROOVE, RIDGE).
• bd: Border width in pixels.
button_close = tk.Button(top_window,
text="Close",
command=top_window.destroy)
button_close.pack()
Pr. Mehdia AJANA 26
Tkinter Toplevel Widget:
PYTHON #Main window
GUI window = tk.Tk()
window.title("Main Window")
button_open = tk.Button(window, text="Open
Toplevel",
command=open_toplevel)
button_open.pack()
Create First
GUI Application window.mainloop()
Tkinter
Create First
GUI Application
Tkinter
Create First
GUI Application
Tkinter
def on_mouse_click(event):
print("Mouse clicked at:", event.x, event.y)
window = tk.Tk()
Create First
window.bind("<Button-1>", on_mouse_click)
GUI Application # Left-click
Tkinter window.mainloop()
window = tk.Tk()
# Label to display the text
label = tk.Label(window, text="Enter text and click
the button")
label.pack(pady=10)
Create First
GUI Application
Tkinter