0% found this document useful (0 votes)
4 views9 pages

Lab Programs Module 6

The document outlines several Python GUI programs created using the tkinter module, including a Combobox, file viewer, Checkbutton widget, and a BMI calculator. Each program includes source code and a description of its functionality and output. The programs demonstrate various GUI components and user interactions in Python applications.

Uploaded by

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

Lab Programs Module 6

The document outlines several Python GUI programs created using the tkinter module, including a Combobox, file viewer, Checkbutton widget, and a BMI calculator. Each program includes source code and a description of its functionality and output. The programs demonstrate various GUI components and user interactions in Python applications.

Uploaded by

sbajibaba99
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Roll No:

PROGRAM NO: 20

Aim: Develop a Python GUI program to create a Combobox with three options using tkinter module

SOURCE CODE:

import tkinter as tk
from tkinter import ttk

root = [Link]()
[Link]("Combobox Example")
[Link]("300x200")
label = [Link](root, text="Select an option:")
[Link](pady=10)
combo = [Link](root)
combo['values'] = ("Option 1", "Option 2", "Option 3")
[Link]("Choose here")
[Link](pady=10)
def show():
label_result.config(text="Selected: " + [Link]())
btn = [Link](root, text="Show Selection", command=show)
[Link](pady=10)
label_result = [Link](root, text="")
label_result.pack()
[Link]()

OUTPUT:

When program is executed, following Screen appears:


Roll No:

When combo box is clicked, a drop down list is appeared as follows:

For example, “option 2” is selected and “Show Selection” button is clicked and then below the
button, text is displayed as “Selected: option2”, shown in the following screen:
Roll No:

PROGRAM NO: 21

Aim: Create a complete Python GUI application that lets the user open a file and view its contents
using Tkinter.

SOURCE CODE:

import tkinter as tk
from tkinter import filedialog

# Function to open file


def open_file():
file_path = [Link](
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if file_path:
with open(file_path, "r") as file:
content = [Link]()
text_area.delete(1.0, [Link])
text_area.insert([Link], content)
status_label.config(text=file_path)

root = [Link]()
[Link]("File Viewer")
[Link]("600x400")
top_frame = [Link](root)
top_frame.pack(pady=5)
open_btn = [Link](top_frame, text="Open File", command=open_file)
open_btn.pack()

frame = [Link](root)
[Link](expand=True, fill="both")
scrollbar = [Link](frame)
[Link](side="right", fill="y")

text_area = [Link](frame, wrap="word", yscrollcommand=[Link])


text_area.pack(expand=True, fill="both")

[Link](command=text_area.yview)
status_label = [Link](root, text="No file opened", anchor="w")
status_label.pack(fill="x", side="bottom")
[Link]()
Roll No:

OUTPUT:

When program is executed, following Screen appears:

Next, Click on the “Open File” Button at the top, then a open dialog box is appeared, browse for the
desired file
Roll No:

Selected file contents are displayed in the text area widget:


Roll No:

PROGRAM NO: 22

Aim: Implement a Python GUI program to create a Checkbutton widget

SOURCE CODE:

import tkinter as tk
def show():
selected = []
if [Link]() == 1:
[Link]("Java")
if [Link]() == 1:
[Link]("Python")
if [Link]() == 1:
[Link]("C++")
if selected:
[Link](text="Selected: " + ", ".join(selected))
else:
[Link](text="No option selected")
root = [Link]()
[Link]("Multiple Checkbuttons")
[Link]("300x250")
var1 = [Link]()
var2 = [Link]()
var3 = [Link]()
cb1 = [Link](root, text="Java", variable=var1)
cb2 = [Link](root, text="Python", variable=var2)
cb3 = [Link](root, text="C++", variable=var3)
[Link](anchor="w")
[Link](anchor="w")
[Link](anchor="w")
btn = [Link](root, text="Show Selection", command=show)
[Link](pady=10)
label = [Link](root, text="")
[Link]()
[Link]()
Roll No:

OUTPUT:

When program is executed, following Screen appears:

Choose any of the options, and Click “Show Selection” Button, then selected check boxes labels are
displayed below the button as shown in the below screen:
Roll No:

PROGRAM NO: 23

Aim: Design and implement a Python GUI application to calculate the Body Mass Index (BMI) of a
user using the Tkinter library. The application should accept the user’s name, weight (in kilograms),
and height (in centimeters) through appropriate input fields, upon clicking a button, the program

BMI value should be calculated using the formula: 𝐵𝑀𝐼 = 𝑊𝑒𝑖𝑔ℎ𝑡 (𝑘𝑔) / (𝐻𝑒𝑖𝑔ℎ𝑡 (𝑚))2
must compute the BMI using the standard formula and display the result clearly on the screen. The

SOURCE CODE:

import tkinter as tk
# Function to calculate BMI
def calculate_bmi():
name = name_entry.get()
weight = weight_entry.get()
height = height_entry.get()
if name == "" or weight == "" or height == "":
result_label.config(text="Please fill all fields")
return
weight = float(weight)
height = float(height)
height_m = height / 100
bmi = weight / (height_m ** 2)
result_label.config(text=f"{name}, your BMI is {bmi:.2f}")

root = [Link]()
[Link]("BMI Calculator")
[Link]("400x250")
[Link](bg="#e6e6e6")
[Link](root, text="BMI Calculator", font=("Arial", 16, "bold"),
bg="#e6e6e6").grid(row=0, column=0, columnspan=2, pady=10)
[Link](root, text="Enter your name:", bg="#e6e6e6").grid(row=1, column=0, padx=10, pady=5,
sticky="e")
name_entry = [Link](root, width=25)
name_entry.grid(row=1, column=1, padx=10, pady=5)
[Link](root, text="Enter your weight (kg):", bg="#e6e6e6").grid(row=2, column=0, padx=10,
pady=5, sticky="e")
weight_entry = [Link](root, width=25)
weight_entry.grid(row=2, column=1, padx=10, pady=5)
[Link](root, text="Enter your height (cm):", bg="#e6e6e6").grid(row=3, column=0, padx=10,
pady=5, sticky="e")
height_entry = [Link](root, width=25)
height_entry.grid(row=3, column=1, padx=10, pady=5)
[Link](root, text="Calculate BMI", bg="green", fg="white",
width=15, command=calculate_bmi).grid(row=4, column=0, columnspan=2, pady=15)
result_label = [Link](root, text="", bg="#e6e6e6", font=("Arial", 11))
result_label.grid(row=5, column=0, columnspan=2)
[Link]()
Roll No:

OUTPUT:

You might also like