python - 让 Tkinter 等到按下按钮

标签 python python-3.x user-interface button tkinter

我有一个游戏,当创建一个按钮时,我需要我的程序只显示这个屏幕,直到他们按下“下一级”,所有这些代码都在一个 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 等到按下按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790449/

相关文章:

python - 从 shell 脚本中表的最后一行中提取值

python - 如何在 Python3 中将字符串数字列表转换为 int 数字?

python-3.x - 确定 Pandas 数组中的唯一用法

android - 如何使整个小部件区域可点击?

c# - 如何实现复杂的 UI 逻辑?

python - 如何从浮雕对象中提取圆形文本

python - 在python beautifulsoup中遍历多个div,输出到df然后csv

java - JTable 非常慢

python - Pandas 如何选择行具有最大值的列?

python - grDevices 保持文件打开