0% found this document useful (0 votes)
6 views6 pages

Python Lab Assignment 8

The document contains a Python lab assignment focused on Tkinter, featuring examples of creating a basic window, a button with a click event, a simple calculator, a login form with validation, and a temperature converter. Each example includes code snippets demonstrating how to implement the respective functionality using Tkinter. The assignment serves as a practical guide for students to learn and apply Tkinter in Python programming.

Uploaded by

kudesiat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Python Lab Assignment 8

The document contains a Python lab assignment focused on Tkinter, featuring examples of creating a basic window, a button with a click event, a simple calculator, a login form with validation, and a temperature converter. Each example includes code snippets demonstrating how to implement the respective functionality using Tkinter. The assignment serves as a practical guide for students to learn and apply Tkinter in Python programming.

Uploaded by

kudesiat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Lab Assignment 8

Topic: Tkinter

1. Basic Tkinter Window

import tkinter as tk

root = tk.Tk()

root.title("Welcome Window")

root.geometry("500x400")

label = tk.Label(root, text="Hello, Tkinter!", font=("Arial", 24))

label.pack(expand=True)

root.mainloop()

2. Button with Click Event

import tkinter as tk

from tkinter import messagebox

def on_button_click():

messagebox.showinfo("Information", "Button was clicked!")

root = tk.Tk()

root.title("Button Click Example")

button = tk.Button(root, text="Press Me", command=on_button_click)

button.pack(pady=20)
root.mainloop()

3. Simple Calculator

import tkinter as tk

def button_click(value):

entry.insert(tk.END, value)

def clear_entry():

entry.delete(0, tk.END)

def calculate():

try:

result = eval(entry.get())

entry.delete(0, tk.END)

entry.insert(0, str(result))

except Exception as e:

entry.delete(0, tk.END)

entry.insert(0, 'Error')

root = tk.Tk()

root.title("Simple Calculator")

entry = tk.Entry(root, width=30, borderwidth=5)

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


buttons = [

'7', '8', '9', '/',

'4', '5', '6', '*',

'1', '2', '3', '-',

'0', 'C', '=', '+'

row_val = 1

col_val = 0

for button in buttons:

if button == 'C':

b = tk.Button(root, text=button, width=10, height=2, command=clear_entry)

elif button == '=':

b = tk.Button(root, text=button, width=10, height=2, command=calculate)

else:

b = tk.Button(root, text=button, width=10, height=2, command=lambda b=button: button_click(b))

b.grid(row=row_val, column=col_val)

col_val += 1

if col_val > 3:

col_val = 0

row_val += 1

root.mainloop()

4. Login Form with Validation

import tkinter as tk
def login():

username = entry_username.get()

password = entry_password.get()

if username == 'admin' and password == 'password123':

result_label.config(text="Login successful", fg='green')

else:

result_label.config(text="Invalid username or password", fg='red')

root = tk.Tk()

root.title("Login Form")

tk.Label(root, text="Username").grid(row=0, column=0, padx=10, pady=10)

entry_username = tk.Entry(root)

entry_username.grid(row=0, column=1, padx=10, pady=10)

tk.Label(root, text="Password").grid(row=1, column=0, padx=10, pady=10)

entry_password = tk.Entry(root, show='*')

entry_password.grid(row=1, column=1, padx=10, pady=10)

login_button = tk.Button(root, text="Login", command=login)

login_button.grid(row=2, column=0, columnspan=2, pady=10)

result_label = tk.Label(root, text="")

result_label.grid(row=3, column=0, columnspan=2)


root.mainloop()

5. Temperature Converter

import tkinter as tk

def celsius_to_fahrenheit():

celsius = float(entry_celsius.get())

fahrenheit = (celsius * 9/5) + 32

entry_fahrenheit.delete(0, tk.END)

entry_fahrenheit.insert(0, str(fahrenheit))

def fahrenheit_to_celsius():

fahrenheit = float(entry_fahrenheit.get())

celsius = (fahrenheit - 32) * 5/9

entry_celsius.delete(0, tk.END)

entry_celsius.insert(0, str(celsius))

root = tk.Tk()

root.title("Temperature Converter")

tk.Label(root, text="Celsius").grid(row=0, column=0, padx=10, pady=10)

entry_celsius = tk.Entry(root)

entry_celsius.grid(row=0, column=1, padx=10, pady=10)

tk.Label(root, text="Fahrenheit").grid(row=1, column=0, padx=10, pady=10)

entry_fahrenheit = tk.Entry(root)

entry_fahrenheit.grid(row=1, column=1, padx=10, pady=10)


btn_c_to_f = tk.Button(root, text="Convert to Fahrenheit", command=celsius_to_fahrenheit)

btn_c_to_f.grid(row=2, column=0, columnspan=2, pady=10)

btn_f_to_c = tk.Button(root, text="Convert to Celsius", command=fahrenheit_to_celsius)

btn_f_to_c.grid(row=3, column=0, columnspan=2, pady=10)

root.mainloop()

You might also like