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

Unit V Graphical User Interfaces

Uploaded by

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

Unit V Graphical User Interfaces

Uploaded by

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

Python Programming: Detailed Explanation

UNIT V: Graphical User Interfaces

GUI Programming with Tkinter

Graphical User Interfaces (GUIs) are essential for user-friendly applications. Tkinter is a Python library for

building desktop applications with a variety of interactive elements.

from tkinter import Tk, Label

# Create a basic Tkinter window

root = Tk()

root.title("Tkinter GUI Example")

label = Label(root, text="Hello, Tkinter!")

label.pack()

root.mainloop()

Using Label Widgets

Label widgets display text or images in a Tkinter window. They can be used to show static information such

as instructions or titles.

from tkinter import Tk, Label

root = Tk()

label = Label(root, text="This is a Label Widget", font=("Arial", 14))

label.pack()

root.mainloop()

Organizing Widgets with Frames


Python Programming: Detailed Explanation

Frames are used to group widgets in Tkinter for better organization and layout control. Frames can be nested

and arranged in grids or packed.

from tkinter import Tk, Frame, Label

root = Tk()

frame = Frame(root)

frame.pack()

label = Label(frame, text="This is inside a Frame")

label.pack()

root.mainloop()

Button Widgets and Info Dialog Boxes

Button widgets in Tkinter trigger actions when clicked. Info dialog boxes provide messages to the user.

from tkinter import Tk, Button, messagebox

def show_message():

messagebox.showinfo("Info", "You clicked the button!")

root = Tk()

button = Button(root, text="Click Me", command=show_message)

button.pack()

root.mainloop()

Getting Input with Entry Widget


Python Programming: Detailed Explanation

The Entry widget is used to get single-line text input from the user.

from tkinter import Tk, Entry, Button

def get_input():

user_input = entry.get()

print(f"User Input: {user_input}")

root = Tk()

entry = Entry(root)

entry.pack()

button = Button(root, text="Submit", command=get_input)

button.pack()

root.mainloop()

Using Labels as Output Fields

Labels can also be used dynamically to display output. After receiving user input, you can modify the text

displayed in a Label widget.

from tkinter import Tk, Label, Button

def update_label():

label.config(text="Updated Text!")

root = Tk()

label = Label(root, text="Original Text")


Python Programming: Detailed Explanation

label.pack()

button = Button(root, text="Update Text", command=update_label)

button.pack()

root.mainloop()

Radio Buttons and Check Buttons

Radio Buttons allow users to select one option from a set of choices, while Check Buttons allow multiple

selections.

from tkinter import Tk, Radiobutton, IntVar, Checkbutton

root = Tk()

radio_value = IntVar()

# Radio Button example

radiobutton1 = Radiobutton(root, text="Option 1", variable=radio_value, value=1)

radiobutton1.pack()

radiobutton2 = Radiobutton(root, text="Option 2", variable=radio_value, value=2)

radiobutton2.pack()

# Check Button example

check_button = Checkbutton(root, text="Accept Terms")

check_button.pack()

root.mainloop()
Python Programming: Detailed Explanation

Simple Graphics with Turtle Graphics

Turtle graphics provides a way to draw shapes and images using a cursor (the 'turtle'). It's widely used for

basic graphics and educational purposes.

from turtle import Turtle, Screen

t = Turtle()

screen = Screen()

# Draw a square

for _ in range(4):

t.forward(100)

t.left(90)

screen.mainloop()

Two-Dimensional Shapes and Colors in Turtle Graphics

Turtle graphics can also be used to draw more complex shapes and control the colors used for drawing.

from turtle import Turtle

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 with Python

Image processing in Python is done using libraries like Pillow (PIL). You can perform tasks like resizing,

rotating, and manipulating images.

from PIL import Image

# Open an image file

image = Image.open("sample.jpg")

image.show()

# Resize image

resized_image = image.resize((200, 200))

resized_image.show()

GUI Case Studies


Python Programming: Detailed Explanation

Building a GUI application often involves combining multiple widgets and functionalities. A case study

involves creating a complete application such as a simple calculator or text editor.

from tkinter import Tk, Button

def add_numbers():

result = int(entry1.get()) + int(entry2.get())

label_result.config(text=f"Result: {result}")

root = Tk()

entry1 = Entry(root)

entry1.pack()

entry2 = Entry(root)

entry2.pack()

button = Button(root, text="Add", command=add_numbers)

button.pack()

label_result = Label(root, text="Result:")

label_result.pack()

root.mainloop()

You might also like