SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Gui Programming
Exercise:
Programming Exercises
Question:6 | ISBN:9780132576376 | Edition: 2

Question

Joe’s Automotive performs the following routine maintenance services:
• Oil change—$26.00
• Lube job—$18.00
• Radiator flush—$30.00
• Transmission flush—$80.00
• Inspection—$15.00
• Muffler replacement—$100.00
• Tire rotation—$20.00
Write a GUI program with check buttons that allow the user to select any or all of these services. When the user clicks a button the total charges should be displayed.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

# import tkinter to create windows app
import tkinter as tk

# Create dictionary to store the prices of each service
service_prices = {
    "Oil change": 26.00,
    "Lube job": 18.00,
    "Radiator flush": 30.00,
    "Transmission flush": 80.00,
    "Inspection": 15.00,
    "Muffler replacement": 100.00,
    "Tire rotation": 20.00
}

# Create a function named calculate_total().
# This function helps to calcualte the total price.
def calculate_total():
    total = 0.00
    for service, price in service_prices.items():
        if service_vars[service].get():
            total += price

    total_label.config(text=f"Total Charges: ${total:.2f}")

# Create the main window
root = tk.Tk()
root.title("Joe's Automotive")
root.geometry("350x300")


# Create variables to store the check button selections
service_vars = {}
for service in service_prices:
    service_vars[service] = tk.BooleanVar()

# Create check buttons for each service
for i, (service, price) in enumerate(service_prices.items()):
    tk.Checkbutton(root, text=f"{service} - ${price:.2f}", variable=service_vars[service]).grid(row=i, sticky="w")

# Create the "Calculate Total" button
calculate_button = tk.Button(root, text="Calculate Total", command=calculate_total)
calculate_button.grid(row=len(service_prices), pady=10)

# Create a label to display the total charges
total_label = tk.Label(root, text="Total Charges: $0.00", font=("Arial", 12, "bold"))
total_label.grid(row=len(service_prices) + 1, pady=10)

root.mainloop()

 

Sample output:

0 0

Discussions

Post the discussion to improve the above solution.