Tkinter Label not appearing

╄→гoц情女王★ 提交于 2021-02-08 05:17:20

问题


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


回答1:


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)

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.



来源:https://stackoverflow.com/questions/44768319/tkinter-label-not-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!