【问题标题】:Tkinter Label not appearingTkinter 标签未出现
【发布时间】:2017-11-29 19:13:55
【问题描述】:

我正在为带有 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)

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

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

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

【问题讨论】:

  • 当我使用 controll C 停止程序或输入随机值以致失去所有生命时,标签出现
  • 我认为您不应该像这样每 1/10 秒调用一次 while 循环。一旦 while 语句开始,它将在每个循环中自行检查 game.health 的值。您可能需要修改此函数的编写方式,以避免一遍又一遍地创建许多 while 语句。
  • @SierraMountainTech:我不明白它是如何每 1/10 秒调用一次的。它看起来只被调用一次,在代码完成初始化后的 1/10 秒。不过,它仍然从根本上被破坏了。
  • game.measureDistance() 来自 def measureDistance(self): self.playerDistance = float(input("Guess a distance: ")) 意思是它不会快速循环,它每次循环一个距离已被测量,但在这种情况下,每次用户输入内容时都会循环测试它。
  • @BryanOakley 我知道root.after 在另一个函数中,因为我帮助他解决了另一个问题。虽然你是对的。再次查看另一个问题后,我没有看到任何显示该函数被多次调用的内容。我一定是看到了什么。

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


【解决方案1】:

您需要在放置标签后更新显示,因为您的代码仍在运行。在不手动运行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)

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

【讨论】:

    【解决方案2】:

    也许你应该尝试在 lbl 的定义下做lbl.pack()。通常它会有所帮助

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多