【发布时间】:2017-12-01 02:34:58
【问题描述】:
我有一个游戏,当一个按钮被创建时,我需要我的程序只显示这个屏幕,直到他们按下“下一级”所有这些代码都在一个 while 循环中,所以在一个大的 while 循环中控制游戏。
......
if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
game.level += 1
showLevelResults(game)
#NextLevelButton
btnNextLevel = Button(root,
#Random Config
command = nextLevel,
)
btnNextLevel.place(x=1003, y=492, anchor=NW, width=247, height=78)
updateMainScreen()
while nextLev == False:
#What Do I put in here to force a wait
else:
......
nextLev = False
def nextLevel():
nextLev = True
...
目前这将它保持在 while 循环中,当按下按钮时没有任何变化,我使用 time.sleep(1) 让它等待并让它打印等待 btn 按下,但是这会向控制台发送垃圾邮件,并且当按下按钮仍然不会改变屏幕。
def showGameSurvival():
game = gamemode_normal()
while game.health != 0:
game.next = False
clearScreen()
changeBackground("Survival")
#Placing Labels on the screen for game.....
#... Health
root.update()
lblCountDownLeft = Label(root, bg="White", fg="Green", font=XXLARGE_BUTTON_FONT)
lblCountDownLeft.place(x=169, y=350, anchor=CENTER)
lblCountDownRight = Label(root, bg="White", fg="Green", font=XXLARGE_BUTTON_FONT)
lblCountDownRight.place(x=1111, y=350, anchor=CENTER)
#CountDown
count = 7
while count > 0:
lblCountDownLeft['text'] = count
lblCountDownRight['text'] = count
root.update()
count -= 1
time.sleep(1)
lblCountDownLeft.destroy()
lblCountDownRight.destroy()
root.update()
#Num on left x=169, right, x=1111 y=360
game.measureDistance()
if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
game.level += 1
clearScreen()
changeBackground("Survival")
graphicalDisplay(game)
#NextLevelButton
btnNextLevel = Button(root,
bg= lbBlue,
fg="white",
text="Level" + str(game.level),
font=SMALL_BUTTON_FONT,
activebackground="white",
activeforeground= lbBlue,
command= lambda: nextLevel(game),
bd=0)
btnNextLevel.place(x=1003, y=492, anchor=NW, width=247, height=78)
root.update()
while game.next == False:
print(game.next)
else:
game.health -= 1
if game.allowance > 4:
game.allowance = int(game.allowance*0.9)
#when game is over delete the shit
if game.health == 0:
del game
下一个按钮现在调用这个函数:def nextLevel(game):
game.next = True
【问题讨论】:
-
不要在 tkinter 中使用
sleep()。请改用after()。使用 sleep 将暂停整个 tkinter 实例,并且可能没有按照您认为的那样做。 -
请创建一个minimal reproducible example。作为一般规则,您应该不惜一切代价避免自己的游戏循环,因为 tkinter 已经有一个无限循环。我们需要看看您是如何实现游戏循环的。
-
嘿,我已经更新了我的问题并删除了我的代码中许多不重要的部分。我只需要我的屏幕保持原样,在创建该按钮后没有任何变化,并且在单击该按钮之前一直保持这种状态,非常感谢!
标签: python python-3.x user-interface button tkinter