0

This is my code:

from tkinter import *

class Car:

    def __init__(self, car_id, name, num_seats, available):

        self.car_id = car_id
        self.name = name
        self.num_seats = num_seats
        self.available = available
        available_cars.append(self.name)


    def hire_car(self):
        self.available = False
        booked_cars.append(self.name)
        available_cars.remove(self.name)



    def return_car(self):
        self.available = True
        booked_cars.remove(self.name)
        available_cars.append(self.name)

    def display():
        pass

class Carlist:

    def __init__(self, available_cars):

        self.available_cars = available_cars

    def addCar():
        pass

    def displayAvailable():
        print(available_cars)


    def displayBooked():
        print(booked_cars)


booked_cars = []
available_cars = []

Car(1,"Suzuki Van", 2, True)
id2 = Car(2,"Toyota Corolla", 4, True)
id3 = Car(3,"Honda Crv", 4, True)
id4 = Car(4,"Suzuki Swift", 4, True)
id5 = Car(5,"Mitsibishi Airtrek", 4, True)
id6 = Car(6,"Nissan DC Ute", 4, True)
id7 = Car(7,"Toyota Previa", 7, True)


Car.hire_car(id3)


Carlist.displayAvailable()
Carlist.displayBooked()


#Interface Code

root = Tk()
root.title("Car Booking System")



frameTop = Frame(root)
frameLeft = Frame(root)
frameRight = Frame(root)
frameBottom = Frame(root)

#Top
topLabel = Label(frameTop, text = "Car Booking System", font = 20).pack()

#Right Side
bookLabel = Label(frameRight, text = "Book:").pack()

varSelectedCar = StringVar()
varSelectedCar.set(available_cars[0])
optionHire = OptionMenu(frameRight, varSelectedCar, *available_cars).pack()

buttonHireCar = Button(frameRight, text = "Enter", command = Car.hire_car).pack()

#Left Side
returnLabel = Label(frameLeft, text = "Return:").pack()

varSelectedCar2 = StringVar()
varSelectedCar2.set(booked_cars[0])
optionReturn = OptionMenu(frameLeft, varSelectedCar2, *booked_cars).pack()

buttonHireCar2 = Button(frameLeft, text = "Enter", command = Car.hire_car).pack()
#Bottom
summaryLabel = Label(frameBottom, text = "Summary:").pack()



#INITIALISATION
frameTop.pack(side = TOP)
frameLeft.pack(side = LEFT)
frameRight.pack(side = RIGHT)
frameBottom.pack(side = BOTTOM)
root.mainloop()

I am trying to link the lists to the GUI interface so when enter is clicked it either runs hire_car or return_car and adds their name to the relevant list. Any idea how to do this? Help would be much appreciated.

The overall idea is to have a program which can book and return cars and as a summary at the bottom....

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Please note that the ttk.Combobox is a more up-to-date UI element than the OptionMenu. `from tkinter.ttk import *` will let you use `combo = Combobox(parent)` and `combo['values'] = ["thing1","thing2"]` lets you set the selection list. – patthoyts Mar 31 '14 at 12:21

1 Answers1

-1

Although this site is not to make other's homework, you got lucky today. However, please try to figure out how it works. How to update OptionMenu info is from HERE.

from collections import namedtuple
from tkinter import *

Car = namedtuple('Car', 'name num_seats')

class CarList:

    ID = 0

    def __init__(self):
        self.cars, self.hired = {}, set()

    def add_car(self, car):
        self.cars[self.ID] = car
        self.ID += 1

    def available_cars(self):
        '''Returns the list of (id, Car) tuples sorted by Car.'''
        return sorted(((car, id)
            for id, car in self.cars.items()
            if id not in self.hired))

    @staticmethod
    def labels(cars_seq):
        return ['{} [{}]'.format(t[0].name, t[1])
            for t in cars_seq]

    def hire_car(self, id):
        if id in self.hired:
            raise ValueError('That car is already hired.')
        self.hired.add(id)

    def hired_cars(self):
        return sorted(((self.cars[id], id)
            for id in self.hired))

    def return_car(self, id):
        if id not in self.hired:
            raise ValueError('That car is not even hired.')        
        self.hired.remove(id)

def add_car(car):
    if not isinstance(car, Car):
        raise TypeError('Invalid car object.')
    carlist.add_car(car)
    update_optionmenus()

def hire_car():
    label = opt_hire.var.get()
    if label:
        id = int(label.rsplit(maxsplit=1)[-1][1:-1])
        carlist.hire_car(id)
        update_optionmenus()

def return_car():
    label = opt_return.var.get()
    if label:
        id = int(label.rsplit(maxsplit=1)[-1][1:-1])
        carlist.return_car(id)
        update_optionmenus()

def _update_optionmenu(opt_menu_widget, car_seq):
    cars = carlist.labels(car_seq)
    if cars:
        opt_menu_widget.var.set(cars[0])
    else:
        opt_menu_widget.var.set('')
    opt_menu_widget['menu'].delete(0, END)
    for lab in cars:
        opt_menu_widget['menu'].add_command(label=lab,
            command=lambda lab=lab: opt_menu_widget.var.set(lab))

def update_optionmenus():
    _update_optionmenu(opt_hire, carlist.available_cars())
    _update_optionmenu(opt_return, carlist.hired_cars())    

# You have to initiate a carlist to make it work.
carlist = CarList()

#Interface Code
root = Tk()
root.title('Car Booking System')
frame_top = Frame(root)
frame_top.pack(side = TOP)
frame_left = Frame(root)
frame_left.pack(side = LEFT)
frame_right = Frame(root)
frame_right.pack(side = RIGHT)
frame_bottom = Frame(root)
frame_bottom.pack(side = BOTTOM)

#Top
label_top = Label(frame_top, text = 'Car Booking System',
    font = 20).pack()

#Right Side
label_hire = Label(frame_right, text = 'Book:', width=25).pack()
gui_var_hire = StringVar()
opt_hire = OptionMenu(frame_right, gui_var_hire, 'anything')
opt_hire.var = gui_var_hire
del gui_var_hire
opt_hire.pack()     
button_hire = Button(frame_right, text = 'Enter',
    command = hire_car).pack()

#Left Side
label_return = Label(frame_left, text = 'Return:', width=25).pack()
gui_var_return = StringVar()
opt_return = OptionMenu(frame_left, gui_var_return, 'anything')
opt_return.var = gui_var_return
opt_return.pack()
del gui_var_return  
button_return = Button(frame_left, text = 'Enter',
    command = return_car).pack()

#Bottom
label_summary = Label(frame_bottom, text = 'Summary:').pack()

add_car(Car('Suzuki Van', 2))
add_car(Car('Toyota Corolla', 4))
add_car(Car('Honda Crv', 4))
add_car(Car('Suzuki Swift', 4))
add_car(Car('Mitsibishi Airtrek', 4))
add_car(Car('Nissan DC Ute', 4))
add_car(Car('Toyota Previa', 7))
add_car(Car('Honda Crv', 2))
root.mainloop()
Community
  • 1
  • 1
SzieberthAdam
  • 3,999
  • 2
  • 23
  • 31
  • Instead of just writing code to be copy-and-pasted, your answer would be better if you describe what you did differently than what is in the question. – Bryan Oakley Mar 31 '14 at 23:15
  • Well I usually do that. However, sometimes I like to code better than to write. And here I could have write alot. I should have kept the code for myself. Next time I will do that. Instead of downvoting my answer, you could write your own matey. – SzieberthAdam Apr 01 '14 at 05:25