Unit V Graphical User Interfaces
Unit V Graphical User Interfaces
Graphical User Interfaces (GUIs) are essential for user-friendly applications. Tkinter is a Python library for
root = Tk()
label.pack()
root.mainloop()
Label widgets display text or images in a Tkinter window. They can be used to show static information such
as instructions or titles.
root = Tk()
label.pack()
root.mainloop()
Frames are used to group widgets in Tkinter for better organization and layout control. Frames can be nested
root = Tk()
frame = Frame(root)
frame.pack()
label.pack()
root.mainloop()
Button widgets in Tkinter trigger actions when clicked. Info dialog boxes provide messages to the user.
def show_message():
root = Tk()
button.pack()
root.mainloop()
The Entry widget is used to get single-line text input from the user.
def get_input():
user_input = entry.get()
root = Tk()
entry = Entry(root)
entry.pack()
button.pack()
root.mainloop()
Labels can also be used dynamically to display output. After receiving user input, you can modify the text
def update_label():
label.config(text="Updated Text!")
root = Tk()
label.pack()
button.pack()
root.mainloop()
Radio Buttons allow users to select one option from a set of choices, while Check Buttons allow multiple
selections.
root = Tk()
radio_value = IntVar()
radiobutton1.pack()
radiobutton2.pack()
check_button.pack()
root.mainloop()
Python Programming: Detailed Explanation
Turtle graphics provides a way to draw shapes and images using a cursor (the 'turtle'). It's widely used for
t = Turtle()
screen = Screen()
# Draw a square
for _ in range(4):
t.forward(100)
t.left(90)
screen.mainloop()
Turtle graphics can also be used to draw more complex shapes and control the colors used for drawing.
t = Turtle()
# Set color
t.color("blue")
# Draw a triangle
Python Programming: Detailed Explanation
for _ in range(3):
t.forward(100)
t.left(120)
t.color("green")
# Draw a square
for _ in range(4):
t.forward(100)
t.left(90)
t.hideturtle()
Image processing in Python is done using libraries like Pillow (PIL). You can perform tasks like resizing,
image = Image.open("sample.jpg")
image.show()
# Resize image
resized_image.show()
Building a GUI application often involves combining multiple widgets and functionalities. A case study
def add_numbers():
label_result.config(text=f"Result: {result}")
root = Tk()
entry1 = Entry(root)
entry1.pack()
entry2 = Entry(root)
entry2.pack()
button.pack()
label_result.pack()
root.mainloop()