Я новичок в Python и Tkinter, и я пытался сделать GUI с лифтом и несколькими кнопками, чтобы перемещать его между этажами. Лифт движется хорошо, но я не могу найти способ обновить номер этажа, когда лифт достигает указанного c этажа, поэтому, когда лифт поднимается на этаж 3 и вы нажимаете кнопку # 1, он поднимается на один этаж вверх вместо два этажа вниз. Кто-нибудь может помочь?
Я пробовал использовать много разных вещей, таких как циклы while и условные выражения, но ничего из этого не помогло. В какой-то момент лифт начал двигаться сам по себе (до нажатия кнопок), поэтому я использовал лямбду в команде кнопки, чтобы исправить это. Единственная проблема сейчас заключается в том, что когда я использую лямбду, операторы возврата в определениях движения не работают, и поэтому номер этажа не обновляется. Кто-нибудь знает, как это исправить?
Python 3.8
извините, мой код немного беспорядок
Вот мой код:
import time
import tkinter as tk
from tkinter import *
Конструкция здания и форма лифта
window = tk.Tk()
window.title("Elevator Animation")
window.geometry("500x600")
window.configure(background="black")
Title = Label (window, text="Elevator", bg="black", fg="white", font="none 14 bold")
Title.pack()
canvas = Canvas(window, width=500, height=500)
canvas.pack()
elevator = canvas.create_rectangle(60, 500, 80, 440, fill="gray")
canvas.create_line(60,20,60,500, fill="black")
canvas.create_line(80,20,80,500, fill="black")
canvas.create_line(480,20,480,500, fill="black")
canvas.create_line(60,20,480,20)
canvas.create_line(60,80,480,80)
canvas.create_line(60,140,480,140)
canvas.create_line(60,200,480,200)
canvas.create_line(60,260,480,260)
canvas.create_line(60,320,480,320)
canvas.create_line(60,380,480,380)
canvas.create_line(60,440,480,440)
canvas.create_line(60,500,480,500)
canvas.create_text(70, 475, fill="black", font="none 10 bold", text="PB")
canvas.create_text(70, 410, fill="black", font="none 10 bold", text="1")
canvas.create_text(70, 350, fill="black", font="none 10 bold", text="2")
canvas.create_text(70, 290, fill="black", font="none 10 bold", text="3")
canvas.create_text(70, 230, fill="black", font="none 10 bold", text="4")
canvas.create_text(70, 170, fill="black", font="none 10 bold", text="5")
canvas.create_text(70, 110, fill="black", font="none 10 bold", text="6")
canvas.create_text(70, 50, fill="black", font="none 10 bold", text="7")
Функции движения
def dontmove(floor):
global new_floor
new_floor = floor
return new_floor
def move1up(floor):
for x in range(0, 12):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 1
return new_floor
def move2up(floor):
for x in range(0, 24):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 2
return new_floor
def move3up(floor):
for x in range(0, 36):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 3
return new_floor
def move4up(floor):
for x in range(0, 48):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 4
return new_floor
def move5up(floor):
for x in range(0, 60):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 5
return new_floor
def move6up(floor):
for x in range(0, 72):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 6
return new_floor
def move7up(floor):
for x in range(0, 12):
canvas.move(1, 0, -5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor + 7
return new_floor
def move1down(floor):
for x in range(0, 12):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 1
return new_floor
def move2down(floor):
for x in range(0, 24):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 2
return new_floor
def move3down(floor):
for x in range(0, 36):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 3
return new_floor
def move4down(floor):
for x in range(0, 48):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 4
return new_floor
def move5down(floor):
for x in range(0, 60):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 5
return new_floor
def move6down(floor):
for x in range(0, 72):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 6
return new_floor
def move7down(floor):
for x in range(0, 12):
canvas.move(1, 0, 5)
window.update()
time.sleep(0.05)
global new_floor
new_floor = floor - 7
return new_floor
Название кнопок
blank1 = Label (window, text="Buttons: ", bg="black", fg="white", font="none 12 bold")
blank1.pack(side=TOP)
blank2 = Label (window, text=" ", bg="black", fg="black", font="none 14 bold")
blank2.pack()
Вот в чем проблема (я думаю):
new_floor = 0
while new_floor == 0:
pb = tk.Button (window, text="PB", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: dontmove(0))
pb.place(x=132,y=560)
floor1 = Button (window, text="1", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1up(0))
floor1.place(x=170,y=560)
floor2 = Button (window, text="2", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2up(0))
floor2.place(x=200,y=560)
floor3 = Button (window, text="3", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move3up(0))
floor3.place(x=230,y=560)
floor4 = Button (window, text="4", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move4up(0))
floor4.place(x=260,y=560)
floor5 = Button (window, text="5", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move5up(0))
floor5.place(x=290,y=560)
floor6 = Button (window, text="6", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move6up(0))
floor6.place(x=320,y=560)
floor7 = Button (window, text="7", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move7up(0))
floor7.place(x=350,y=560)
break
while new_floor == 1:
pb = tk.Button (window, text="PB", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1down(1))
pb.place(x=132,y=560)
floor1 = Button (window, text="1", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: dontmove(1))
floor1.place(x=170,y=560)
floor2 = Button (window, text="2", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1up(1))
floor2.place(x=200,y=560)
floor3 = Button (window, text="3", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2up(1))
floor3.place(x=230,y=560)
floor4 = Button (window, text="4", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move3up(1))
floor4.place(x=260,y=560)
floor5 = Button (window, text="5", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move4up(1))
floor5.place(x=290,y=560)
floor6 = Button (window, text="6", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move5up(1))
floor6.place(x=320,y=560)
floor7 = Button (window, text="7", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move6up(1))
floor7.place(x=350,y=560)
break
while new_floor == 2:
pb = tk.Button (window, text="PB", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2down(2))
pb.place(x=132,y=560)
floor1 = Button (window, text="1", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1down(2))
floor1.place(x=170,y=560)
floor2 = Button (window, text="2", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: dontmove(2))
floor2.place(x=200,y=560)
floor3 = Button (window, text="3", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1up(2))
floor3.place(x=230,y=560)
floor4 = Button (window, text="4", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2up(2))
floor4.place(x=260,y=560)
floor5 = Button (window, text="5", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move3up(2))
floor5.place(x=290,y=560)
floor6 = Button (window, text="6", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move4up(2))
floor6.place(x=320,y=560)
floor7 = Button (window, text="7", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move5up(2))
floor7.place(x=350,y=560)
break
while new_floor == 3:
pb = tk.Button (window, text="PB", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move3down(3))
pb.place(x=132,y=560)
floor1 = Button (window, text="1", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2down(3))
floor1.place(x=170,y=560)
floor2 = Button (window, text="2", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1down(3))
floor2.place(x=200,y=560)
floor3 = Button (window, text="3", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: dontmove(3))
floor3.place(x=230,y=560)
floor4 = Button (window, text="4", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move1up(3))
floor4.place(x=260,y=560)
floor5 = Button (window, text="5", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move2up(3))
floor5.place(x=290,y=560)
floor6 = Button (window, text="6", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move3up(3))
floor6.place(x=320,y=560)
floor7 = Button (window, text="7", bg="black", fg="green", highlightbackground="yellow", activebackground="blue", command=lambda: move4up(3))
floor7.place(x=350,y=560)
break
mainloop()