The button in question is supposed to calculate the cost of a car rental, from inputs for how many months, how many weeks and how many days.
This is the entire code at the moment:

import tkinter as tk
from datetime import *


# Create a tkinter window
window = tk.Tk()
window.title("Car Rental Service")
window.resizable(True, True)

# Define global variables
rental_cost = 0
rental_duration = 0

def submit_form():
    name = name_entry.get()


# Define functions
def calculate_cost():
      # Extract numeric values from label text
    rental_days_text = rental_label_days.cget('text')
    rental_days = 0
    if any(c.isdigit() for c in rental_days_text):
        rental_days = int(''.join(filter(str.isdigit, rental_days_text)))

    rental_weeks_text = rental_label_weeks.cget('text')
    rental_weeks = 0
    if any(c.isdigit() for c in rental_weeks_text):
        rental_weeks = int(''.join(filter(str.isdigit, rental_weeks_text)))

    rental_months_text = rental_label_months.cget('text')
    rental_months = 0
    if any(c.isdigit() for c in rental_months_text):
        rental_months = int(''.join(filter(str.isdigit, rental_months_text)))

    global rental_cost
    # Calculate rental cost based on duration
    if rental_days >= 1:
        rental_cost = 30 * rental_days
    elif rental_weeks >= 1:
        rental_cost = 95 * rental_weeks
    elif rental_months >= 1:
        rental_cost = 270 * rental_months

    # Add VAT to rental cost
    rental_cost = ((rental_days * 36) + (rental_weeks * 114) + (rental_months * 324)) * 1.2
    # Update the cost label
    cost_label.config(text="Total cost: £" + str(round(rental_cost, 2)))

def rent_car():
    # Get customer details
    name = name_entry.get()
    dob = dob_entry.get()
    nationality = nationality_entry.get()
    licence_duration = licence_entry.get()
    # Display rental details
    rental_label.config(text="Thank you, " + name + ". You have rented a " + category_var.get() + " car for " + str(rental_duration) + " " + duration_var.get_name() + ".")
    # Disable form fields
    name_entry.config(state="disabled")
    dob_entry.config(state="disabled")
    nationality_entry.config(state="disabled")
    licence_entry.config(state="disabled")
    category_small.config(state="disabled")
    category_medium.config(state="disabled")
    category_large.config(state="disabled")
    one_day.config(state="disabled")
    one_week.config(state="disabled")
    one_month.config(state="disabled")
    # Enable the checkout button
    checkout_button.config(state="normal")

def validate_age():
    dob = dob_entry.get()
    age = calculate_age(dob)
    if age < 24:
        tk.Label(text="You must be aged 24 or over to complete this rental form.")
        submit_button.config(state="disabled")
    else:
        tk.Label(text="")
        submit_button.config(state="normal")

def checkout():
    # Display checkout message
    checkout_label.config(text="Thank you for your custom. You have been charged £" + str(round(rental_cost, 2)) + ".")

def calculate_age(dob):
    # Calculate age from date of birth
    # Assuming dob is in the format "dd/mm/yyyy"
    dob_list = dob.split("/")
    dob_day = int(dob_list[0])
    dob_month = int(dob_list[1])
    dob_year = int(dob_list[2])
    today = date.today()
    age = today.year - dob_year - ((today.month, today.day) < (dob_month, dob_day))
    return age

small_car_img = tk.PhotoImage(file="small-car (1).gif")
medium_car_img = tk.PhotoImage(file="mid-size-car.gif")
large_car_img = tk.PhotoImage(file="large-car (1).gif")

def update_image():
    if category_var.get() == "Small":
        image_label.config(image=small_car_img)
    elif category_var.get() == "Medium":
        image_label.config(image=medium_car_img)
    elif category_var.get() == "Large":
        image_label.config(image=large_car_img)

# Create form fields
name_label = tk.Label(window, text="Name:")
name_label.pack()
name_entry = tk.Entry(window)
name_entry.pack()

dob_label = tk.Label(window, text="Date of birth (dd/mm/yyyy):")
dob_label.pack()
dob_entry = tk.Entry(window)
dob_entry.pack()

validate_age_button = tk.Button(window, text="Validate Age", command=validate_age)
validate_age_button.pack()

nationality_label = tk.Label(window, text="Nationality:")
nationality_label.pack()
nationality_entry = tk.Entry(window)
nationality_entry.pack()

licence_label = tk.Label(window, text="How long have you held your licence for? (years):")
licence_label.pack()
licence_entry = tk.Entry(window)
licence_entry.pack()

label = tk.Label(window, text="Select a car category:")
label.pack()

category_var = tk.StringVar()
small_button = tk.Radiobutton(window, text="Small", variable=category_var, value="Small")
medium_button = tk.Radiobutton(window, text="Medium", variable=category_var, value="Medium")
large_button = tk.Radiobutton(window, text="Large", variable=category_var, value="Large")

image_label = tk.Label(window)
image_label.pack()

small_button.pack()
medium_button.pack()
large_button.pack()

small_button.config(command=update_image)
medium_button.config(command=update_image)
large_button.config(command=update_image)

rental_label_months = tk.Label(window, text="How many months do you plan on renting the car for?:")
rental_label_months.pack()
rental_label_months_entry = tk.Entry(window)
rental_label_months_entry.pack()

rental_label_weeks = tk.Label(window, text="How many weeks do you plan on renting the car for?:")
rental_label_weeks.pack()
rental_label_weeks_entry = tk.Entry(window)
rental_label_weeks_entry.pack()

rental_label_days = tk.Label(window, text="How many days do you plan on renting the car for?:")
rental_label_days.pack()
rental_label_days_entry = tk.Entry(window)
rental_label_days_entry.pack()

cost_label = tk.Label(window, text="Your Total Cost Is: ")
cost_label.pack()
print(cost_label)
cost_label.config(text="Total cost: £" + str(round(rental_cost, 2)))

rental_cost_button = tk.Button(window, text="Calculate Cost", command=calculate_cost)
rental_cost_button.pack()

submit_button = tk.Button(window, text="Submit Form", command=submit_form)
submit_button.pack()

window.mainloop()

Recommended Answers

All 3 Replies

For example, if you do a breakpoint at the line after

rental_days_text = rental_label_days.cget('text')

you'll find that rental_days_text actually has the value

How many days do you plan on renting the car for?:

I don't use tkinter (I use wxPython) so I can't tell you the correct tkinter code but it looks like you are getting the value of the label rather than the value of the entry field.

Additionally, when I run your code, all of the radio buttons for car size selection are selected by default. Again, not knowing tkinter I would guess that these three radio buttons should be put inside a container so that they behave like they should (only one selectable at a time).

Start with tkinter basics. As already stated, the Label isn't going to change, so using cget() on a label does not provide any useful data. Second, start with one get. You have 178 lines of code, and a lot of it is wrong and has to be changed. Take any online tutorial, see "Putting it all together" at http://python-textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html and again, start with one thing. Once that works you can expand the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.