PYTHON ASSIGNMENT
NAME:- TARUN
SRN :- PES1UG24CA160
QUESTION 1:-
CODE:-
import tkinter as tk
root= tk.Tk()
root.title("Label Styling Excersise")
bold_label = tk.Label(root, text="Bold Text", font=("Bold", 12, "bold"))
bold_label.pack()
italic_label = tk.Label(root, text="Italic Text", font=("Helvetica", 12, "italic"))
italic_label.pack()
underline_label = tk.Label(root, text="Underline Text", font=("Helvetica", 12, "underline"))
underline_label.pack()
color_label = tk.Label(root, text="PES1UG24CA160", fg="blue", bg="yellow")
color_label.pack()
right_aligned_label = tk.Label(root, text="RIGHT ALIGNED", anchor="e", width=20)
right_aligned_label.pack()
aligned_label = tk.Label(root, text="Center Aligned", anchor="center")
aligned_label.pack()
border_label = tk.Label(root, text="With Border", relief="solid")
border_label.pack()
label = tk.Label(root, text="Padded Text", padx=10, pady=10)
label.pack()
root.mainloop()OUTPUT:-
QUESTION 2:-
CODE
import tkinter as tk
root = tk.Tk()
root.title("Interactive Button States")
root.geometry("300x300")
def toggle_button():
if button1['state'] == tk.NORMAL:
button1.config(state=tk.DISABLED, text="Disabled")
else:
button1.config(state=tk.NORMAL, text="Enabled")
def change_relief(relief_style):
button1.config(relief=relief_style)
button1 = tk.Button(root, text="Enabled", bg="lightblue", fg="black",
activebackground="green", activeforeground="white",
relief="raised", padx=10, pady=5, state=tk.NORMAL)
button1.pack(pady=20)
toggle_button_state = tk.Button(root, text="Toggle State", command=toggle_button)
toggle_button_state.pack(pady=10)
relief_styles = ["flat", "raised", "sunken", "groove", "ridge"]
for style in relief_styles:
button_style = tk.Button(root, text=style.capitalize(), command=lambda s=style: change_relief(s))
button_style.pack(pady=5)
root.mainloop()
OUTPUT:-
QUESTION 3:-
import tkinter as tk
root = tk.Tk()
root.title("25/10/24 - Experiencial learning Q3_")
counter = 0
def increment_counter():
global counter
counter += 1
counter_button.config(text=f"Count: {counter}_")
counter_button = tk.Button(root, text=f"Count: {counter}_", command=increment_counter)
counter_button.pack(pady=20)
root.mainloop()
OUTPUT:-
QUESTION 4:-
CODE:-
import tkinter as tk
def click_button(value):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current + str(value))
def clear_entry():
entry.delete(0, tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
root = tk.Tk()
root.title("Calculator")
entry = tk.Entry(root, width=16, font=('Arial', 24), justify='right')
entry.grid(row=0, column=0, columnspan=4)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
row_val = 1
col_val = 0
for button in buttons:
if button == '=':
btn = tk.Button(root, text=button, command=calculate, width=5, height=2)
else:
btn = tk.Button(root, text=button, command=lambda b=button: click_button(b), width=5,
height=2)
btn.grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
clear_btn = tk.Button(root, text='C', command=clear_entry, width=30, height=2)
clear_btn.grid(row=row_val, column=0, columnspan=4)
root.mainloop()
OUTPUT:-
QUESTION 5:-
CODE:-
import tkinter as tk
def change_label_text():
label.config(text="TEXT CHANGED!!")
root = tk.Tk()
root.title("Change Label Text Example")
label = tk.Label(root, text="ORIGINAL TEXT")
label.pack(pady=20)
button = tk.Button(root, text="Change Text", command=change_label_text)
button.pack(pady=10)
root.mainloop()
OUTPUT:-