【问题标题】:invalid command name ".!label3" [duplicate]无效的命令名称“.!label3”[重复]
【发布时间】:2021-05-15 17:09:44
【问题描述】:

我正在创建一个游戏,当疫苗达到 100 时,游戏结束,用户被转移到胜利屏幕。见下文

import random
from tkinter import *

#creates new window
root = Tk()

#makes backdrop picture of map
C = Canvas(root, bg="black", height=780, width=1347)
C.grid(row=0,column=0)


#vaccine label creation and place
vaccineLabel=Label(root, text="10000", font ="algerian 25", bg = "Light Blue")
vaccineLabel.place(x=271 ,y=706, relwidth=1/5, relheight=0.1)


totalDeaths = 0
totalPopulation = 1
vaccineCount = 95

#loops until total deaths = population
def simulate_Count():


    def update_Count():
        
        #calls global variables
        global vaccineCount


        #vaccine determination
        vaccine1 = random.randint(0,0)
        vaccine2 = random.randint(0,0)
        if vaccine1 == vaccine2:
            vaccineCount += 1

        #updates labels
        vaccineLabel.config(text = f'Deaths:{vaccineCount}')

        
        if vaccineCount == 100:
            def victory_Screen():

                #calls global root and deletes
                global root
                root.destroy()                           

                #creates the window
                root = Tk()

                #assembles the dimension of the window, and the colour
                C=Canvas(root, bg="black", height=780, width=1347)
                C.grid(row=0, column=0)
                
                #creates a label which will print the game title and places it in the correct dimensions
                victoryLabel=Label(root, text=f"YOU HAVE WON! \n IT ONLY TOOK YOU", bg="black", fg="light grey")
                victoryLabel.place(x=0, y=0, relwidth=1, relheight=1)
            victory_Screen()
            
                
    update_Count()


    #loops until deaths = population
    if totalDeaths < totalPopulation:
        root.after(1000, simulate_Count)

simulate_Count()

这给我带来了一个错误。当我销毁窗口并创建一个新窗口时,会显示新窗口。但是,由于某种原因,这些行中出现错误类型为invalid command name ".!label3"

包含错误的行如下:

#updates labels
vaccineLabel.config(text = f'Vaccine: {vaccineCount}%')

错误似乎是 update_Count() 仍在尝试配置不存在的标签。任何帮助将不胜感激!

【问题讨论】:

  • 请提供预期的minimal, reproducible example (MRE)。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。显示中间结果与您的预期不同的地方。我严重怀疑这篇 130 行的帖子是否能最直接地说明您的问题。
  • 这是我相信的最小可重现示例。我把它从 500 行减少到 130 行。从 15 个对象缩小到只有 2 个。并且需要类来进行计数。所有这些代码都很重要,并且是最小的可重现示例
  • 您的问题是关闭一个窗口并创建另一个窗口。您声称所有这些内部游戏机制对于显示特性都至关重要?
  • 是的。我拥有的所有代码都分别与标签相关,这是关闭窗口时的问题所在。
  • 我现在已经编辑了代码,使它只关注一个标签,因为我猜测它是否是一个错误,对所有标签都是一样的。这使我能够摆脱类和对象。希望这更容易理解。对不起,如果不是之前

标签: python python-3.x tkinter label window


【解决方案1】:

错误被抛出是因为vicion_screen 函数破坏了包含疫苗标签的根窗口。结果,一旦根被销毁,Label 将不再存在,因此您不再在其上使用 .config。

要解决此问题,请在运行 vaccineLabel.config(text = f'Deaths:{vaccineCount}') 之前检查疫苗计数是否

这可以确保在调用 .config 之前疫苗标签仍然存在。

【讨论】:

  • 谢谢你。非常感谢您的帮助。错误现已被克服!
  • 很高兴听到这个消息。
猜你喜欢
  • 1970-01-01
  • 2021-08-15
  • 2011-05-14
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多