如何解决GUI问题(place_forget,图片位置,GUI无响应)?

How to solve GUI problems (place_forget, picture place, GUI not responding )?

label2 = Label(root, text="STOP!", font=font)
label3 = Label(root, text="WAIT!", font=font)
label4 = Label(root, text="GO!", font=font)

TrafficLightOff = Image.open(__file__+"/../bg1.png")
photo = ImageTk.PhotoImage(TrafficLightOff)
TrafficLight = Label(root, image = photo, background="white")
TrafficLight.pack(side=TOP)

TrafficLightRed= Image.open(__file__+"/../bg2.png")
photo1 = ImageTk.PhotoImage(TrafficLightRed)
TrafficLightONRed = Label(root, image = photo1, background="white")

TrafficLightYellow= Image.open(__file__+"/../bg3.png")
photo2 = ImageTk.PhotoImage(TrafficLightYellow)
TrafficLightONYellow = Label(root, image = photo2, background="white")

TrafficLightGreen= Image.open(__file__+"/../bg4.png")
photo3 = ImageTk.PhotoImage(TrafficLightGreen)
TrafficLightONGreen = Label(root, image = photo3, background="white")

def STARTSTOP():
    if STARTSTOPBtn['text']=='START':
        STARTSTOPBtn['text']='STOP'
        TrafficLight.place_forget()
        while True:
            if STARTSTOPBtn['text']=='STOP':
                TrafficLightONRed.pack(side=TOP)
                print("red")
            
                label2.place(x=200, y=100, relwidth=5, relheight=.08, anchor="center")
                time.sleep(15)
                label2.place_forget()
                TrafficLightONRed.pack_forget()

            
                TrafficLightONYellow.pack(side=TOP)
                print("yellow")
            
                label3.place(x=200, y=100, relwidth=5, relheight=.08, anchor="center")
                time.sleep(15)
                label3.place_forget()
                TrafficLightONYellow.pack_forget()

            
                TrafficLightONGreen.pack(side=TOP)
                print("Green")
            
                label4.place(x=200, y=100, relwidth=5, relheight=.08, anchor="center")
                time.sleep(15)
                label4.place_forget()
                # TrafficLightONGreen.pack_forget()
            else:
                break



    elif STARTSTOPBtn['text']=='STOP':
        STARTSTOPBtn['text']='START'
        TrafficLight.pack(side=TOP)


STARTSTOPBtn =Button(root, text="START", font=font, command=STARTSTOP, relief="ridge")
STARTSTOPBtn.place(x=200, y=620, relwidth=.2, relheight=.08, anchor="center")

我已经提交了 Tkinter 申请,但我遇到了一些问题,例如

  1. 我给了TrafficLight.place_forget()但是没有去掉图片的地方
  2. 我做了一个 sleep 的无限循环 15 秒,但它没有给我下一张图片和标签,它只提供打印功能
  3. 当我按下开始按钮时,我的 GUI 没有响应。

请帮我解决这个问题

容易解决。一切都在按预期进行,图像正在更改,位置和标签忘记正在发生,但它是在无限循环中发生的,并且在任何地方都没有据说更新根。所以你看不到发生了什么。如果像下面的示例一样在每个 time.sleep 之前放置 root.update(),它将起作用:

label2.place(x=200, y=100, relwidth=5, relheight=.08, anchor="center")
root.update()
time.sleep(5)
label2.place_forget()
TrafficLightONRed.pack_forget()

GUI 没有响应,因为 15 秒的时间很长,而且在 time.sleep 期间您无法与 GUI 交互