python – 按下Tkinter等待直到按钮
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 按下Tkinter等待直到按钮,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2803字,纯文字阅读大概需要5分钟。
内容图文
![python – 按下Tkinter等待直到按钮](/upload/InfoBanner/zyjiaocheng/757/fb9bcc68977846ccb3f40fd7f812ba23.jpg)
我有一个游戏,当一个按钮被创建时,我需要我的程序只显示这个屏幕,直到他们按下“下一级”所有这些代码都在一个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等待某个事件的最简单方法是调用其中一个“等待”函数,例如wait_variable,wait_window或wait_visibility.
在您的情况下,您希望等待按钮单击,因此您可以使用wait_variable,然后让按钮设置变量.当您单击该按钮时,将设置该变量,并且当设置该变量时,将返回对wait_variable的调用.
例如:
import tkinter as tk
root = tk.Tk()
...
var = tk.IntVar()
button = tk.Button(root, text="Click Me", command=lambda: var.set(1))
button.place(relx=.5, rely=.5, anchor="c")
print("waiting...")
button.wait_variable(var)
print("done waiting.")
注意:您不必使用IntVar – 任何特殊的Tkinter变量都可以.此外,无论您将其设置为什么,该方法都会等到它发生变化.
内容总结
以上是互联网集市为您收集整理的python – 按下Tkinter等待直到按钮全部内容,希望文章能够帮你解决python – 按下Tkinter等待直到按钮所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。