Python Forum
[Tkinter] Tried to programm a parking lot
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tried to programm a parking lot
#1
Hi everyone, so this is my first programm on Tkinter and I tried to simulate cars in a parking lot but I failed to make all the cars move. I just wanna know if I can simplify my code. Thanks for helping me.
PS: Im sorry but most of my functions and variables are in french
from tkinter import *
from random import *
from timeit import default_timer
import time

# Initial conditions of the creation of the parking lot 
nbcase = 5
case = 80
x0, y0 = 80, 0
vitesse = 10
# Initial positions of the car at the parking entrance
coords_entrée = (0, x0)
# Creating window, parking, parking canvas, and dialog box 

def Parking():
    # Closing the initial conditions window
    fenetre.destroy()
    # Creation of the function giving the position when clicking

    def donne_position(event):
        i = (event.x//80)
        j = (event.y//80)+1
        if j == 3:
            TexteC.delete("0.0", END) 
            TexteC.insert(END, "X="+str(i) + " Y=" + str(2))
        if j == 1:
            TexteC.delete("0.0", END)  
            TexteC.insert(END, "X="+str(i) + " Y=" + str(1))
    # Creation window, parking, canvas and widgets boxs 
    fenetre2 = Tk()
    fenetre2.geometry('800x400+0+0')
    fenetre2.title('Parking')
    Cadre_Parking = Frame(fenetre2)
    TexteC = Text(fenetre2, height=2, width=10)
    Can = Canvas(Cadre_Parking, height=241, width=560, bg="white")
    Texte_Position = Label(fenetre2, text="Coordonnées de la case")
    Texte_Vitesse = Label(fenetre2, text='Vitesse de la voiture')
    
    # Initial positions of the initial cars in the car park
    coords_init = (0, y0)
    # Car moving speed
    vit = DoubleVar()
    Choix_Vitesse_X = Spinbox(fenetre2, from_=2, to=20, increment=2, textvariable=vit)
    # Creation and positioning of the initial cars according to the choices
    Liste_Conditions_Initiales = Conditions_Initiales()
    for i in range(len(Liste_Conditions_Initiales)):
        if Liste_Conditions_Initiales[i] == 1:
            rectangle2 = Can.create_rectangle(0, 0, 80, 80, fill="khaki", width=0)
            if i == 0 or i == 2 or i == 4 or i == 6 or i == 8:
                Can.coords(rectangle2, coords_init[0]+x0*(i/2+1), coords_init[1],
                           coords_init[0]+(x0*(i/2+1))+x0, coords_init[1]+x0)
            if i == 1 or i == 3 or i == 5 or i == 7 or i == 9:
                Can.coords(rectangle2, coords_init[0]+x0*(i/2+1/2), coords_init[1]+2*x0,
                           coords_init[0]+(x0*(i/2+1/2))+x0, coords_init[1]+2*x0+x0)
    #Creation of the panel indicating the free places
    nombre= Nombre_place_libres(Liste_Conditions_Initiales)
    Panneau = Label(fenetre2,font=('sans', 10, 'bold'), text='There is {} free places'.format(nombre))
#Creation of the indication panel
    place = minimum(Liste_Conditions_Initiales)+1
    Indication= Label(fenetre2,font=('sans', 10, 'bold'), text='please park at the place {}'.format(place))                
# Window positions, parking, canvas and box widgets
    Cadre_Parking.place(x=0, y=0, width=800, height=500)
    Can.place(x=0, y=0)
    Choix_Vitesse_X.place(x=650, y=110, width=70, height=25)
    TexteC.place(x=650, y=50, width=70, height=25)
    Can.bind("<Button-1>", donne_position)
    Texte_Position.place(x=620, y=20, width=140, height=25)
    Texte_Vitesse.place(x=620, y=80, width=140, height=25)
    Panneau.place(x=200, y=260, width=150, height=25)
    Indication.place(x=160, y=290, width=250, height=25)
# Creating parking spot numbers
    Can.create_text(120,40,text='1',font=('Helvetica',14,'bold')),Can.create_text(120,200,text='2',font=('Helvetica',14,'bold'))
    Can.create_text(200,40,text='3',font=('Helvetica',14,'bold')),Can.create_text(200,200,text='4',font=('Helvetica',14,'bold'))
    Can.create_text(280,40,text='5',font=('Helvetica',14,'bold')),Can.create_text(280,200,text='6',font=('Helvetica',14,'bold'))
    Can.create_text(360,40,text='7',font=('Helvetica',14,'bold')),Can.create_text(360,200,text='8',font=('Helvetica',14,'bold'))
    Can.create_text(440,40,text='9',font=('Helvetica',14,'bold')),Can.create_text(440,200,text='10',font=('Helvetica',14,'bold'))        
# Creation of lines allowing the formation of the car park
    for i in range(0, 6):
        Can.create_line(x0+case*i, y0, x0+case*i, y0 + case)
    for i in range(0, 2):
        Can.create_line(x0, y0+case*i, x0+nbcase*case, y0+case*i)
    for i in range(0, 6):
        Can.create_line(x0+case*i, (2*x0), x0+case*i, (2*x0) + case)
    for i in range(2, 4):
        Can.create_line(x0, y0+case*i, x0+nbcase*case, y0+case*i)
# Creation of the car
    global rectangle
    rectangle = Can.create_rectangle(0, 0, 80, 80, fill="light green", width=0)
    Can.coords(rectangle, coords_entrée[0]+1, coords_entrée[1]+1,
               coords_entrée[0]+80, coords_entrée[1]+80)
# moving the car
    def Deplacement2():  # Choice of speeds and displacement limits according to initial conditions
        L = Conditions_Initiales()
        place = minimum(L)
        L1 = []
        lim_hor, lim_ver, dy2 = 1, 1, 0
        if place == 0 or place == 2 or place == 4 or place == 6 or place == 8:
            lim_hor = 79+80*(place/2+1)
            lim_ver = 0
            dy2 = -1*(vit.get())/2
            L1 = [dy2, lim_ver, lim_hor]
        if place == 1 or place == 3 or place == 5 or place == 7 or place == 9:
            lim_hor = 79+80*(place/2+1/2)
            lim_ver = 279
            dy2 = (vit.get())/2
            L1 = [dy2, lim_ver, lim_hor]
        return(L1)
    def Deplacement_voiture():
        # Random choice of the car moved according to the initial conditions
        global voiture_aléa
        Liste_Conditions_Initiales = Conditions_Initiales()
        Liste_Voiture_Gare = []
        for i in range(len(Liste_Conditions_Initiales)):
            if Liste_Conditions_Initiales[i] == 1:
                Liste_Voiture_Gare.append(i)
        voiture_garé = len(Liste_Voiture_Gare)
        voiture_aléa = randint(1, voiture_garé)
        def Deplacement1():  # Moving the incoming car according to the free places
            dx1 = vit.get()
            dy1 = 0
            dx2 = 0
            dy2 = 0
            L = Deplacement2()
            lim_ver = L[1]
            lim_hor = L[2]
            if Can.coords(rectangle)[2] > lim_hor:
                dx1 = 0
            Can.move(rectangle, dx1, dy1)
            if dx1 == 0:
                dy2 = L[0]
                if dy2 < 0:
                    if Can.coords(rectangle)[1] < 2:
                        dy2 = 0
                        Can.itemconfigure(rectangle, fill='khaki')
                elif dy2 > 0:
                    if Can.coords(rectangle)[3] > 239:
                        dy2 = 0
                        Can.itemconfigure(rectangle, fill='khaki')
                Can.move(rectangle, dx2, dy2)               
            def Deplacement_voiture_garé():  # Moving the randomly chosen car to exit
                dxp = 0
                dyp = 0
                if dx1 == 0 and dy2 == 0:
                    warning= Label(fenetre2,font=('sans', 10, 'bold'), text='warning, outgoing car!')
                    warning.place(x=160, y=320, width=250, height=25)
                    Can.itemconfigure(voiture_aléa, fill='light green')
                    if 0 <= Can.coords(voiture_aléa)[1] <= 79:
                        dyp = vit.get()/2
                    if 161 <= Can.coords(voiture_aléa)[3] <= 240:
                        dyp = -1*vit.get()/2
                    if dyp == 0:
                        dxp = vit.get()
                    if Can.coords(voiture_aléa)[2] == 560:
                        dxp = 0
                        Can.itemconfigure(voiture_aléa, fill='white')
                        rectangle = Can.create_rectangle(0, 0, 80, 80, fill="light green", width=0)
                        Can.coords(rectangle, coords_entrée[0]+1, coords_entrée[1]+1, coords_entrée[0]+80, coords_entrée[1]+80)
                        Can.move(rectangle, dxp, dyp)
                Can.move(voiture_aléa, dxp, dyp)
            Deplacement_voiture_garé()
            fenetre2.after(50, Deplacement1)
        # Creation of the stopwatch
        def lancer_chrono():
            global depart, flag
            flag = 1
            depart = time.time()
            top_horloge()
            stoper_chrono()
        def top_horloge():
            global depart, flag
            global minutes, secondes
            y = time.time()-depart
            minutes = time.localtime(y)[4]
            secondes = time.localtime(y)[5]
            if flag:
                message.configure(text="%i min %i sec " % (minutes, secondes))
            fenetre2.after(1000, top_horloge)
        def stoper_chrono():
            if Can.coords(voiture_aléa)[2] == 560:
                message2.configure(text="%i min %i sec " % (minutes, secondes))
            if Can.coords(voiture_aléa)[2] != 560:
                fenetre2.after(100, stoper_chrono)
        flag = 0
        depart = 0
        message = Label(fenetre2, font=('sans', 20, 'bold'), text="Chrono prêt")
        message2 = Label(fenetre2, font=('sans', 20, 'bold'), text="Test")
        message.place(x=600, y=260, width=200, height=25)
        message2.place(x=600, y=290, width=200, height=25)
        lancer_chrono()
        Deplacement1()
    def Relancer():
        fenetre2.destroy()
    # Creating and positioning the move simulation button
    Bouton_Relancer = Button(fenetre2, text='Relancer la simulation', fg='red', command=Relancer)
    Bouton_Relancer.place(x=620, y=200, width=120, height=25)
    Bouton_Lancer = Button(fenetre2, text="Lancer la simulation",
                           fg="red", command=Deplacement_voiture)
    Bouton_Lancer.place(x=620, y=140, width=120, height=25)
# Creating the function that retrieves the variables from the checkboxes
def Conditions_Initiales():
    L = [var1, var2, var3, var4, var5, var6, var7, var8, var9, var10]
    Liste_Conditions_Initiales = []
    for i in range(10):
        Liste_Conditions_Initiales.append(L[i].get())
    return Liste_Conditions_Initiales
# Function that returns the number of free places
def Nombre_place_libres(L):
    cpt=0
    for i in L:
        if i==0:
            cpt +=1
    return cpt        
# last minimum of the place list
def minimum(L1):
    minim = L1[0]
    place = 0
    for i in range(1, len(L1)):
        if L1[i] <= minim:
            minim = L1[i]
            place = i
    return(place)
# Creation of a window
fenetre = Tk()
fenetre.title('Parking')
# Creation of a title
titre = Label(fenetre, text="Simulation d'un parking 10 places à sens unique")
titre.grid(row=1, column=2)
# Creation of the guideline
consigne = Label(fenetre, text="Choose the initial free spots", fg="blue")
consigne.grid(row=2, column=2)
#Creating control variables for checkboxes
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
# Creating checkboxes
pos_in1 = Checkbutton(fenetre, text="Place 1", variable=var1)
pos_in2 = Checkbutton(fenetre, text="Place 2", variable=var2)
pos_in3 = Checkbutton(fenetre, text="Place 3", variable=var3)
pos_in4 = Checkbutton(fenetre, text="Place 4", variable=var4)
pos_in5 = Checkbutton(fenetre, text="Place 5", variable=var5)
pos_in6 = Checkbutton(fenetre, text="Place 6", variable=var6)
pos_in7 = Checkbutton(fenetre, text="Place 7", variable=var7)
pos_in8 = Checkbutton(fenetre, text="Place 8", variable=var8)
pos_in9 = Checkbutton(fenetre, text="Place 9", variable=var9)
pos_in10 = Checkbutton(fenetre, text="Place 10", variable=var10)
pos_in1.grid(row=3, column=1),  pos_in2.grid(row=3, column=2),  pos_in3.grid(row=3, column=3)
pos_in4.grid(row=4, column=1),  pos_in5.grid(row=4, column=2),  pos_in6.grid(row=4, column=3)
pos_in7.grid(row=5, column=1),  pos_in8.grid(row=5, column=2),  pos_in9.grid(row=5, column=3)
pos_in10.grid(row=6, column=2)
# Creation of the launch button to launch the simulation
Lancer = Button(fenetre, text="Lancer", fg="red", command=Parking)
Lancer.grid(row=7, column=2)
fenetre.mainloop()
Reply


Messages In This Thread
Tried to programm a parking lot - by Croshihw - Sep-19-2018, 09:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Closing the Programm after stuck in while loop Rigunas 3 2,860 Dec-17-2018, 10:57 AM
Last Post: Rigunas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020