Tkinter 标签未出现 [英] Tkinter Label not appearing

查看:27
本文介绍了Tkinter 标签未出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a game for a raspberry pi with a 10inch touch screen, I have decided to use Tkinter to be able to interact with the pi. Currently once the user has selected their game mode, and they are taken to this screen, I then try and place a label on this screen showing their current level, the code runs, tkinter window stops responding however code in the commandline continues. Here is the code that is ran on the Survival Screen:

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)

所以上面所有的调用都是从:

So all of the above is called from:

game = gamemode_normal()
root.after(100, lambda: start_survival(game))

如果有人对可能出现的问题有任何想法,请分享!谢谢你,汤姆

If anyone has any ideas on what might be the problem, please share! Thank you, Tom

推荐答案

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

You need to update the display after placing the label, as your code is still running. Without manually running root.update(), Tkinter waits for your code to finish before updating the contents of the display. That's why it appears when you end the program with Ctrl-C. Here is the new code:

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)

<小时>

许多人将 update 函数与 mainloop 函数混淆.这两个函数的区别在于mainloop函数blocks,意味着它不允许代码在被调用后继续运行——通过在内部运行一个无限循环功能.然而,update 函数只运行循环显示所有内容所需的次数,然后结束并继续您的代码.


Many people confuse the update function with the mainloop function. The difference between these two functions is that the mainloop function blocks, meaning that it doesn't allow the code to continue running after it is called - by running an infinite loop inside the function. However the update function runs the loop only the amount of times needed to display everything, then ends and continues your code.

这篇关于Tkinter 标签未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆