how exactly would I get an age validator and a rental cost calculator to work (python tkinter)?
the code I used for the age validator is as follows:

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")

and the code for the rental cost calculator is as follows:

def calculate_cost():
    global rental_cost
    # Calculate rental cost based on duration
    if rental_label_days >= 1:
        rental_cost = 30 * rental_label_days
    elif rental_label_weeks >= 1:
        rental_cost = 95 * rental_label_weeks
    elif rental_label_months >= 1:
        rental_cost = 270 * rental_label_months

    # Add VAT to rental cost
    rental_cost = rental_cost + (rental_cost * 0.2)
    # Update the cost label
    cost_label.config(text="Total cost: £" + str(round(rental_cost, 2)))

but the error i get when I try to use these are as follows:
age validator:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\corey hopkins\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:/Users/corey hopkins/Documents/IT Unit 4/Car Rental Form.py", line 56, in validate_age
    submit_button.config(state="disabled")
    ^^^^^^^^^^^^^
NameError: name 'submit_button' is not defined

rental cost calculator:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\corey hopkins\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:/Users/corey hopkins/Documents/IT Unit 4/Car Rental Form.py", line 17, in calculate_cost
    if rental_label_days >= 1:
       ^^^^^^^^^^^^^^^^^^^^^^
TypeError: '>=' not supported between instances of 'Label' and 'int'

Where are these lines 17 and 56?

In the first code given line 6 and 9 might be referring to non-existent button. See if the button exists and is the name which you used.

commented: I'll see if I can put the entire code up if that would help? +0
import tkinter as tk
from datetime import *


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

# Define global variables
rental_cost = 0
rental_duration = 0

# Define functions
def calculate_cost():
    global rental_cost
    # Calculate rental cost based on duration
    if rental_label_days >= 1:
        rental_cost = 30 * rental_label_days
    elif rental_label_weeks >= 1:
        rental_cost = 95 * rental_label_weeks
    elif rental_label_months >= 1:
        rental_cost = 270 * rental_label_months

    # Add VAT to rental cost
    rental_cost = rental_cost + (rental_cost * 0.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()

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

window.mainloop()

Unless the way you do the duration is required, I suggest you instead ask the user to select a start and end date. Typically a person would say "I need a car from April 7 to May 10".

commented: This is one of the requirements, unfortunately: 3. Duration of rental (1 day, 1 week, 1 month) and associated cost (£30 + VAT, £95 + VAT, £270 + VAT). +0

I won't debug that but it looks like you are acting on an item before it exists. Example would be line 56. Does submit_button exist when this line is executed?

commented: Doesn't look like it. To fix that tho, I think I would have to move that down to the end, and add a submit button at the end. +0

Edit: validate age button now works :)
Thx.
Now just the rental cost calculator button to fix.

commented: Good job. Best you fix it so you know what was wrong. In short, can't apply actions on objects that aren't there. +17

I know what the issue is with the rental cost calculation button, I just dont know how to fix it.

Little bit of an update: there are no more errors that appear when I try to calculate the rental cost, but it just doesn't calculate the rental cost.

It's time to debug. In the function calculate_cost, add code to print what variables are used to find where the trouble is.

I can't tell what dev system you use but on some you set a break on the line in question so you can see what the variables are. But not everyone has a dev system so you print.

commented: oh, I didnt see that u had replied, sry about that, but it's all been fixed now :) thank u & everyone else for the help. much appreciated :) +0
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.