1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
import tkinter as tk
class Car:
def __init__(self, name):
self.name = name
self.is_available = True
self.borrower = ""
self.reason = ""
class ApplicationLocationVoitures:
def __init__(self, master):
self.master = master
master.title("Gestion de location de voitures")
self.car1 = Car("Voiture 1")
self.car2 = Car("Voiture 2")
self.car3 = Car("Voiture 3")
self.car_list = [self.car1, self.car2, self.car3]
# Frame pour afficher les informations de location
self.info_frame = tk.Frame(master)
self.info_frame.pack()
# Création des libellés pour afficher les informations de location
self.car_label = tk.Label(self.info_frame, text="Voiture")
self.car_label.grid(row=0, column=0, padx=5, pady=5)
self.borrower_label = tk.Label(self.info_frame, text="Emprunteur")
self.borrower_label.grid(row=0, column=1, padx=5, pady=5)
self.reason_label = tk.Label(self.info_frame, text="Raison")
self.reason_label.grid(row=0, column=2, padx=5, pady=5)
self.availability_label = tk.Label(self.info_frame, text="Disponibilité")
self.availability_label.grid(row=0, column=3, padx=5, pady=5)
# Frame pour afficher les boutons pour emprunter et rendre une voiture
self.button_frame = tk.Frame(master)
self.button_frame.pack()
self.borrow_button = tk.Button(self.button_frame, text="Emprunter", command=self.borrow_car)
self.borrow_button.pack(side="left", padx=5, pady=5)
self.return_button = tk.Button(self.button_frame, text="Rendre", command=self.return_car)
self.return_button.pack(side="left", padx=5, pady=5)
# Création des étiquettes pour afficher les informations de chaque voiture
for i, car in enumerate(self.car_list):
car_name_label = tk.Label(self.info_frame, text=car.name)
car_name_label.grid(row=i+1, column=0, padx=5, pady=5)
borrower_label = tk.Label(self.info_frame, text=car.borrower)
borrower_label.grid(row=i+1, column=1, padx=5, pady=5)
reason_label = tk.Label(self.info_frame, text=car.reason)
reason_label.grid(row=i+1, column=2, padx=5, pady=5)
availability_label = tk.Label(self.info_frame, text="Disponible" if car.is_available else "Occupé")
availability_label.grid(row=i+1, column=3, padx=5, pady=5, sticky="w")
if car.is_available:
availability_label.config(fg="green")
else:
availability_label.config(fg="red")
def return_car(self):
# Ouvre une boîte de dialogue pour sélectionner la voiture à rendre
input_dialog = ReturnDialog(self.master, self.car_list)
self.master.wait_window(input_dialog.top)
# Met à jour les informations de la voiture
if input_dialog.result:
selected_car = input_dialog.selected_car.get()
for car in self.car_list:
if car.name == selected_car:
car.is_available = True
car.borrower = ""
car.reason = ""
# Met à jour les informations affichées
self.update_car_info()
def update_car_info(self):
# Met à jour les informations affichées pour chaque voiture
for i, car in enumerate(self.car_list):
for child in self.info_frame.winfo_children():
if int(child.grid_info()["row"]) == i+1 and int(child.grid_info()["column"]) == 1:
child.config(text=car.borrower)
if int(child.grid_info()["row"]) == i+1 and int(child.grid_info()["column"]) == 2:
child.config(text=car.reason)
if int(child.grid_info()["row"]) == i+1 and int(child.grid_info()["column"]) == 3:
child.config(text="Disponible" if car.is_available else "Occupé")
if car.is_available:
child.config(fg="green")
else:
child.config(fg="red")
class Car:
def __init__(self, name):
self.name = name
self.is_available = True
self.borrower = ""
self.reason = ""
class BorrowDialog:
def __init__(self, parent):
self.top = tk.Toplevel(parent)
self.selected_car = tk.StringVar()
self.borrower = tk.StringVar()
self.reason = tk.StringVar()
car_choices = [car.name for car in parent.car_list if car.is_available]
if not car_choices:
car_choices = [car.name for car in parent.car_list]
# Création des widgets pour la boîte de dialogue
car_label = tk.Label(self.top, text="Voiture:")
car_label.grid(row=0, column=0, padx=5, pady=5)
self.car_optionmenu = tk.OptionMenu(self.top, self.selected_car, *car_choices)
self.car_optionmenu.grid(row=0, column=1, padx=5, pady=5)
borrower_label = tk.Label(self.top, text="Emprunteur:")
borrower_label.grid(row=1, column=0, padx=5, pady=5)
borrower_entry = tk.Entry(self.top, textvariable=self.borrower)
borrower_entry.grid(row=1, column=1, padx=5, pady=5)
reason_label = tk.Label(self.top, text="Raison:")
reason_label.grid(row=2, column=0, padx=5, pady=5)
reason_entry = tk.Entry(self.top, textvariable=self.reason)
reason_entry.grid(row=2, column=1, padx=5, pady=5)
button_frame = tk.Frame(self.top)
button_frame.grid(row=3, column=1, padx=5, pady=5)
ok_button = tk.Button(button_frame, text="OK", command=self.ok)
ok_button.grid(row=0, column=0, padx=5, pady=5)
cancel_button = tk.Button(button_frame, text="Annuler", command=self.cancel)
cancel_button.grid(row=0, column=1, padx=5, pady=5)
# Empêche la boîte de dialogue de se fermer sans avoir validé ou annulé
self.top.grab_set()
def ok(self):
# Vérifie que toutes les informations ont été saisies
if not self.selected_car.get() or not self.borrower.get() or not self.reason.get():
messagebox.showerror("Erreur", "Toutes les informations sont requises.")
else:
self.result = True
self.top.destroy()
def cancel(self):
self.result = False
self.top.destroy()
class ReturnDialog:
class BorrowDialog:
def __init__(self, parent, car_list):
self.top = tk.Toplevel(parent)
self.selected_car = tk.StringVar()
car_choices = [car.name for car in car_list if car.is_available]
# Création des widgets pour la boîte de dialogue
car_label = tk.Label(self.top, text="Voiture:")
car_label.grid(row=0, column=0, padx=5, pady=5)
self.car_optionmenu = tk.OptionMenu(self.top, self.selected_car, *car_choices)
self.car_optionmenu.grid(row=0, column=1, padx=5, pady=5)
button_frame = tk.Frame(self.top)
button_frame.grid(row=1, column=1, padx=5, pady=5)
ok_button = tk.Button(button_frame, text="OK", command=self.ok)
ok_button.grid(row=0, column=0, padx=5, pady=5)
cancel_button = tk.Button(button_frame, text="Annuler", command=self.cancel)
cancel_button.grid(row=0, column=1, padx=5, pady=5)
# Empêche la boîte de dialogue de se fermer sans avoir validé ou annulé
self.top.grab_set()
def ok(self):
# Vérifie que toutes les informations ont été saisies
if not self.selected_car.get():
messagebox.showerror("Erreur", "Veuillez sélectionner une voiture.")
else:
self.result = True
self.top.destroy()
def cancel(self):
self.result = False
self.top.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = CarRentalApp(root)
root.mainloop() |
Partager