Python Tkinter标签未出现

Python Tkinter标签未出现,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我正在为一个10英寸触摸屏的树莓圆周率制作一个游戏,我决定使用Tkinter来与圆周率进行交互。 目前,一旦用户选择了他们的游戏模式,并且他们被带到这个屏幕,我就会尝试在这个屏幕上放置一个标签,显示他们当前的级别,代码运行,tkinter窗口停止响应,但是命令行中的代码继续。 以下是生存屏幕上运行的代码: def start_survival(game): while game.health != 0: print(str(game.level))#for Testi

我正在为一个10英寸触摸屏的树莓圆周率制作一个游戏,我决定使用Tkinter来与圆周率进行交互。 目前,一旦用户选择了他们的游戏模式,并且他们被带到这个屏幕,我就会尝试在这个屏幕上放置一个标签,显示他们当前的级别,代码运行,tkinter窗口停止响应,但是命令行中的代码继续。 以下是生存屏幕上运行的代码:

def start_survival(game):
    while game.health != 0:  
        print(str(game.level))#for Testing

        lbl = Label(root, text=lbBlue, font=SMALL_BUTTON_FONT)
        lbl['text'] = 'level: ' + str(game.level)
        lbl.place(x=35, y=15)
        print('Where is: ' + str(game.newDistance())+ ' and allowance is: ' + str(game.allowance))

        #game.newDistance()
        #count down
        game.measureDistance()
        if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
            game.level += 1
            print('NEXT LEVEL')
        else:
            game.health -= 1
            print('Ouch! You just lost a life your health is now: ' + str(game.health))
            #u guessed ..... which is not in the range ..... ---->  little diagram
        game.allowance = int(game.allowance*0.90)

        if game.allowance > 5:
            game.allowance = int(game.allowance*0.90)
如果有人对可能出现的问题有任何想法,请分享!
谢谢,Tom

您需要在放置标签后更新显示,因为您的代码仍在运行。如果不手动运行
root.update()
,Tkinter将在更新显示内容之前等待代码完成。这就是为什么在使用Ctrl-C结束程序时会出现此代码。以下是新代码:

def start_survival(game):
    while game.health != 0:  
        print(str(game.level))#for Testing

        lbl = Label(root, text=lbBlue, font=SMALL_BUTTON_FONT)
        lbl['text'] = 'level: ' + str(game.level)
        lbl.place(x=35, y=15)
        root.update()
        print('Where is: ' + str(game.newDistance())+ ' and allowance is: ' + str(game.allowance))

        #game.newDistance()
        #count down
        game.measureDistance()
        if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
            game.level += 1
            print('NEXT LEVEL')
        else:
            game.health -= 1
            print('Ouch! You just lost a life your health is now: ' + str(game.health))
            #u guessed ..... which is not in the range ..... ---->  little diagram
        game.allowance = int(game.allowance*0.90)

        if game.allowance > 5:
            game.allowance = int(game.allowance*0.90)
def开始_生存(游戏):
而游戏。健康!=0:  
打印(str(游戏等级))#用于测试
lbl=标签(根,文本=lbBlue,字体=SMALL\u按钮\u字体)
lbl['text']='level:'+str(game.level)
杠杆位置(x=35,y=15)
root.update()
打印('Where is:'+str(game.newDistance())+'和余量是:'+str(game.allowment))
#game.newDistance()
#倒计时
game.measureDistance()
如果game.playerDistance>=game.lowerBound()和game.playerDistance小图
游戏津贴=整数(游戏津贴*0.90)
如果游戏津贴>5:
游戏津贴=整数(游戏津贴*0.90)


许多人混淆了
update
函数和
mainloop
函数。这两个函数之间的区别在于
mainloop
函数块,这意味着它不允许代码在被调用后继续运行-通过在函数内部运行无限循环。但是,
update
函数只运行循环显示所有内容所需的时间,然后结束并继续您的代码。

也许您应该尝试在lbl定义下执行
lbl.pack()
。通常情况下,当我用controll C停止程序时,或者如果我输入随机值使我失去了所有的活力,标签就会出现。我认为你不应该像这样每隔1/10秒调用一次while循环。while语句开始后,它将在自己的每个循环中检查
game.health
的值。您可能需要修改此函数的编写方式,以避免一次又一次地创建许多while语句。@SierraMountainTech:我看不出它是如何每隔十分之一秒调用一次的。它看起来只被调用一次,即代码完成初始化后的十分之一秒。但是它仍然从根本上被破坏。game.measureDistance()来自def measureDistance(self):self.playerdDistance=float(输入(“猜测距离”)),这意味着它不会快速循环,每次测量距离时它都会循环,但在本例中,为了测试它,每次用户输入内容时它都会循环。@BryanOakley我知道
root.after
在另一个函数中,因为我帮助他解决了另一个问题。虽然你是对的。在再次查看另一个问题后,我没有看到任何显示函数被多次调用的内容。我一定看到了什么。
def start_survival(game):
    while game.health != 0:  
        print(str(game.level))#for Testing

        lbl = Label(root, text=lbBlue, font=SMALL_BUTTON_FONT)
        lbl['text'] = 'level: ' + str(game.level)
        lbl.place(x=35, y=15)
        root.update()
        print('Where is: ' + str(game.newDistance())+ ' and allowance is: ' + str(game.allowance))

        #game.newDistance()
        #count down
        game.measureDistance()
        if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
            game.level += 1
            print('NEXT LEVEL')
        else:
            game.health -= 1
            print('Ouch! You just lost a life your health is now: ' + str(game.health))
            #u guessed ..... which is not in the range ..... ---->  little diagram
        game.allowance = int(game.allowance*0.90)

        if game.allowance > 5:
            game.allowance = int(game.allowance*0.90)