Question: Hello, this is a car rental program using Tkinter Gui with Python. I want to be able to integrate a scrollbar for the car model
Hello, this is a car rental program using Tkinter Gui with Python. I want to be able to integrate a scrollbar for the car model area (under settings), where users get to pick from the options listed when they type the car make. For example, when user enters "Tesla", the options for Tesla models pop up under the car model scrollbar, so they can choose what they are looking for, instead of having the user to type out the model. Could you please show me a code example of how it's done? Thanks!
Here is my program so far:
from tkinter import * from tkinter import messagebox ''' A Car Class is create which contains a list of all the available car information in the form of List Car has following variables : isSelected : Stores if a car is selected in the settings frame selectedCar : Stores the car from the list which matches the user preference car_list : It has a list of available car with attributes as car make, car model, colour, distance, and image url ''' class Car: ''' init function initializes all the variables to their default value ''' def __init__(self): self.isSelected = False self.selectedCar = None self.car_list = [ { "car_make": "Hyundai", "car_model": "Hyundai Tucson", "colour": "Black", "distance": 123, "image": "hyundaitucson.png" }, { "car_make": "Ford", "car_model": "Ford Fusion", "colour": "Blue", "distance": 123, "image": "fordfusion.png" }, { "car_make": "Tesla", "car_model": "Tesla Model 3", "colour": "White", "distance": 233, "image": "teslamodel3.png" } ] def selectCar(self, car_make, car_model, colour, distance): for car in self.car_list: if car_make.lower() == car["car_make"].lower(): if car_model.lower() == car["car_model"].lower(): if colour.lower() == car["colour"].lower(): if (car["distance"]-50) < int(distance) < (car["distance"]+50): self.selectedCar = car self.isSelected = True return True self.isSelected = False return False ''' NewWindow is new window created on top of the master window ''' class NewWindow(Toplevel): def __init__(self, master=None, car=None): def valuesEntered(): ''' Calls the select car method of the car object for finding a matching car If matching car is found than its information is added to master frame Otherwise warning is shown that No Matching Car found ''' if car.selectCar(car_make.get(), car_model.get(), colour.get(), distance.get()): ''' calls the addCar function of master frame so that car information can be added to master frame ''' addCar() else: messagebox.showwarning("Warning", "No Matching Car Found") super().__init__(master=master) self.title("Settings") self.geometry("500x500") ''' Following variables stores the data entered by the user in the Entry box StringVar() is a function of tkinter for storing the user input. It is passed as textvariable to Input Boxes of tkinter. ''' car_make = StringVar() car_model = StringVar() colour = StringVar() distance = StringVar() self.columnconfigure(0, weight=1) # Creating settings entries self.label1 = Label(self, text="Car Make: ", font=("Arial", 14)) self.label1.grid(row=0, column=0) self.entry1 = Entry(self, width="20", font=( "Arial", 14), textvariable=car_make) self.entry1.grid(row=10, column=0) self.label2 = Label(self, text="Car Model: ", font=("Arial", 14)) self.label2.grid(padx=(0, 10)) self.entry2 = Entry(self, width="20", font=( "Arial", 14), textvariable=car_model) self.entry2.grid() self.label3 = Label(self, text="Colour: ", font=("Arial", 14)) self.label3.grid(padx=(0, 10)) self.entry3 = Entry(self, width="20", font=( "Arial", 14), textvariable=colour) self.entry3.grid() self.label4 = Label(self, text="Distance (Km): ", font=("Arial", 14)) self.label4.grid(padx=(0, 10)) w = Spinbox(self, from_=0, to=500, textvariable=distance) w.grid() self.button1 = Button(self, text="Search", command=valuesEntered) self.button1.grid() self.button2 = Button(self, text="Quit", command=self.master.destroy) self.button2.grid() ''' Creates a car instance which stores the current car object''' searched_car = Car() # Main Window master = Tk() master.geometry("500x500") master.title("Airbnb Car Rentals") master.columnconfigure(0, weight=1) # making background image canvas = Canvas(master, width=1000, height=1000) canvas.pack(fill=BOTH, expand=1) imag = "car_background.png" img = PhotoImage(file=imag) canvas.create_image(0, 0, anchor=NW, image=img) label = Label(canvas, text="Search Car", fg="brown", font=("Arial", 30)) label.pack(side="top") # a button widget which will # open a new window on button click btn = Button(canvas, text="Settings", font=("Arial", 20)) # Following line will bind click event # On any click left / right button # of mouse a new window will be opened btn.bind(" Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
