<legend id='vl3YH'><style id='vl3YH'><dir id='vl3YH'><q id='vl3YH'></q></dir></style></legend>
    • <bdo id='vl3YH'></bdo><ul id='vl3YH'></ul>
    <i id='vl3YH'><tr id='vl3YH'><dt id='vl3YH'><q id='vl3YH'><span id='vl3YH'><b id='vl3YH'><form id='vl3YH'><ins id='vl3YH'></ins><ul id='vl3YH'></ul><sub id='vl3YH'></sub></form><legend id='vl3YH'></legend><bdo id='vl3YH'><pre id='vl3YH'><center id='vl3YH'></center></pre></bdo></b><th id='vl3YH'></th></span></q></dt></tr></i><div id='vl3YH'><tfoot id='vl3YH'></tfoot><dl id='vl3YH'><fieldset id='vl3YH'></fieldset></dl></div>

    <small id='vl3YH'></small><noframes id='vl3YH'>

  1. <tfoot id='vl3YH'></tfoot>

      让Tkinter等待直到按钮被按下

      Making Tkinter wait untill button is pressed(让Tkinter等待直到按钮被按下)

      <tfoot id='bc6lj'></tfoot><legend id='bc6lj'><style id='bc6lj'><dir id='bc6lj'><q id='bc6lj'></q></dir></style></legend>

          <tbody id='bc6lj'></tbody>

            • <bdo id='bc6lj'></bdo><ul id='bc6lj'></ul>

                <small id='bc6lj'></small><noframes id='bc6lj'>

                <i id='bc6lj'><tr id='bc6lj'><dt id='bc6lj'><q id='bc6lj'><span id='bc6lj'><b id='bc6lj'><form id='bc6lj'><ins id='bc6lj'></ins><ul id='bc6lj'></ul><sub id='bc6lj'></sub></form><legend id='bc6lj'></legend><bdo id='bc6lj'><pre id='bc6lj'><center id='bc6lj'></center></pre></bdo></b><th id='bc6lj'></th></span></q></dt></tr></i><div id='bc6lj'><tfoot id='bc6lj'></tfoot><dl id='bc6lj'><fieldset id='bc6lj'></fieldset></dl></div>
              1. 本文介绍了让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循环中,当按下按钮时,我使用的时间没有任何变化。睡眠(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变量都可以。此外,您将其设置为什么并不重要,该方法将一直等到其更改。

                这篇关于让Tkinter等待直到按钮被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                How to break out of multiple loops?(如何打破多个循环?)
                How to put the legend out of the plot(如何将传说从情节中剔除)
                Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                • <i id='nVFgF'><tr id='nVFgF'><dt id='nVFgF'><q id='nVFgF'><span id='nVFgF'><b id='nVFgF'><form id='nVFgF'><ins id='nVFgF'></ins><ul id='nVFgF'></ul><sub id='nVFgF'></sub></form><legend id='nVFgF'></legend><bdo id='nVFgF'><pre id='nVFgF'><center id='nVFgF'></center></pre></bdo></b><th id='nVFgF'></th></span></q></dt></tr></i><div id='nVFgF'><tfoot id='nVFgF'></tfoot><dl id='nVFgF'><fieldset id='nVFgF'></fieldset></dl></div>

                  <tfoot id='nVFgF'></tfoot>

                      1. <legend id='nVFgF'><style id='nVFgF'><dir id='nVFgF'><q id='nVFgF'></q></dir></style></legend>
                        • <bdo id='nVFgF'></bdo><ul id='nVFgF'></ul>
                            <tbody id='nVFgF'></tbody>
                        • <small id='nVFgF'></small><noframes id='nVFgF'>