1

I am creating a game where when the vaccine reaches 100, the game finishes, and the user is transferred to a victory screen. See below

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()

This brought me an error. When I destroy the window and create a new one, the new window displays. However, for some reason an error occurred in these lines with error type invalid command name ".!label3"

The lines which contained the errors are below:

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

The error seems to be that update_Count() is still trying to configure a label that doesn't exist. Any help would be much appreciated!!

  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Show where the intermediate results differ from what you expected. I seriously doubt that this 130-line post is the most direct way to illustrate your problem. – Prune Feb 11 '21 at 22:09
  • This is the minimal reproducible example I believe. I brought it down from 500 lines to 130. Narrowed down from 15 objects to just 2. And the class is required to do the count. All this code is important, and is the minimal reproducible example – Aran Khalastchi Feb 11 '21 at 22:12
  • Your question is on closing one window and creating another. You claim that *all* of these internal game mechanics are critical to the display characterstics? – Prune Feb 11 '21 at 22:17
  • Yes. All the code I have is relevant to the labels respectively which is where the issue lies when closing the window. – Aran Khalastchi Feb 11 '21 at 22:19
  • I have now edited the code so that it only focuses on one label because I'm guessing if it's an error for one, its the same for all of them. This has made me able to get rid of the class and objects. Hopefully this is easier to understand. Sorry if it wasn't before – Aran Khalastchi Feb 11 '21 at 22:28
  • That looks *much* better. Now, can you make it self-contained? This dies because the background file is not on our machines. I'm trying to help you catch someone who knows more about Tkinter than I do ... :-) – Prune Feb 11 '21 at 22:30
  • I think that should do it! I also think I know the error but I don't know how to correct it. The function update_Count() is still trying to configure a label that doesn't exist. – Aran Khalastchi Feb 11 '21 at 22:32
  • Yes, that does it for me, too. I've rescinded my closure vote and reversed my down-vote. Now all we need is someone who recognizes the magic label ... – Prune Feb 11 '21 at 22:44
  • The problem is because you're calling `root = Tk()` twice. – martineau Feb 11 '21 at 23:03

1 Answers1

0

The error is being thrown because the victory_screen function destroys the root window which contains the vaccineLabel. As a result, once the root is destroyed the Label no longer exists and so you are no longer to use .config on it.

To fix this, check if the vaccineCount < 100 before you run vaccineLabel.config(text = f'Deaths:{vaccineCount}').

This ensures that the vaccineLabel still exists before calling .config on it.