我有一个游戏的代码,你必须及时点击蚊子。如果不这样做,则会丢失并可以通过单击“重新启动”按钮重新启动。但当我到达那一点时,窗口关闭并重新打开,我无法点击任何东西。我该如何解决? (注意:我是Python新手)
from tkinter import *
from random import randint
from time import *
def replace():
global score
global initialized
global start
end = time()
timeneeded = end - start
label.config(text="")
if timeneeded > (2-score/20) and initialized > 0:
lose()
else:
z1=randint(0,950)
z2=randint(0,660)
button.place(x=z1,y=z2)
start = time()
initialized = 1
score=score+1
def lose():
text="Diesmal hast Du zu lange gebraucht!\n"
text+="Du hast insgesamt "+str(score)+" Mücken erwischt!"
label.config(text=text)
label.pack(side=LEFT,anchor=CENTER,fill=BOTH)
restartButton=Button(window, text = "Nochmal?", command = restart())
restartButton.place(x=700, y=350)
def restart():
global start
global score
global initialized
global mosquito
global z1
global z2
global button
global label
global window
try:
if('normal' == window.state()):
window.destroy()
finally:
score = 0
initialized = 0
start=time()
window =Tk()
# mosquito=PhotoImage(file="/home/pi/Downloads/mosq45.png")
mosquito=PhotoImage(file="C:/Users/Julian/Downloads/mosq45.png")
z1=350
z2=350
initialized = 0
start=time()
score = 0
window.title("Fang die Mücke!")
window.geometry("1020x740")
button=Button(window,image=mosquito,command=replace)
button.place(x=z1,y=z2)
label=Label(window,font=("Arial",32),text="Fang so viele Mücken wie möglich!")
label.place(x=10,y=200)
window.mainloop()
window =Tk()
# mosquito=PhotoImage(file="/home/pi/Downloads/mosq45.png")
mosquito=PhotoImage(file="C:/Users/Julian/Downloads/mosq45.png")
z1=350
z2=350
initialized = 0
start=time()
score = 0
window.title("Fang die Mücke!")
window.geometry("1020x740")
button=Button(window,image=mosquito,command=replace)
button.place(x=z1,y=z2)
label=Label(window,font=("Arial",32),text="Fang so viele Mücken wie möglich!")
label.place(x=10,y=200)
window.mainloop()
答案 0 :(得分:0)
在主机中定义刷新/重启功能。销毁根,然后重新启动该过程。将此功能绑定到主GUI中的任何按钮。希望这会有所帮助。
import sys
from tkinter import *
def vp_start_gui():
global root
root = Tk()
""" INSERT YOUR WHOLE CODE HERE"""
""" CREATE A BUTTON AND ITS command = refresh() which is defined at the bottom"""
root.mainloop()
if __name__ == '__main__':
def refresh():
root.destroy()
vp_start_gui()
vp_start_gui()