1

I'm making a program that makes variables with a base name and variables like their id and series. I use vars()[str(name+id+serie)] to make them and make it a button using the tkinter module. When I launch it, it works until it tries to .get() the value from it, saying

keyError (variable name)

I tried to change how it's named, making it int() or moving the .get() here and there but nothing works.

# -*- coding: utf-8 -*

from tkinter import *
import math
import random

fenetre = Tk()
fenetre.geometry("1000x1000")
kanvas=Canvas(fenetre, width=500, height=500, bg="white")

id = 0
serie = 1
idcounter=0

while 1:

    print("serie =",serie)
    def cheezegrater():
        global serie,id,idcounter

        vars()[str("var_cheeze_sum"+str(serie))]=0
        for o in range(1,val+1):
            print("var11 =",var_cheeze_value11.get())
            vars()[str("var_cheeze_sum"+str(serie))] +=  vars()[str("var_cheeze_value"+str(id-val+o)+str(serie))].get()

        kanvas.pack()
        fenetre.mainloop()
    vars()[str("nombre_de_formes"+str(serie))] =int(float(input("combien?")))

    val = vars()[str("nombre_de_formes"+str(serie))]

    for o in range(1,val+1):
        id+=1
        vars()[str("var_cheeze_value"+str(id)+str(serie))] = Entry(kanvas, width=10)
        o+=1
        vars()[str("var_cheeze_value"+str(id)+str(serie))].pack

        kanvas.pack()
        fenetre.mainloop()
Traceback (most recent call last):
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Utilisateur/.PyCharmEdu2019.1/config/scratches/scratch_1.py", line 38, in cheezegrater
vars()[str("var_cheeze_sum"+str(serie))] +=  vars()[str("var_cheeze_value"+str(id-val+o)+str(serie))].get()
KeyError: 'var_cheeze_value11'
finefoot
  • 9,914
  • 7
  • 59
  • 102
  • 1
    Possible duplicate of [Python vars() global name error](https://stackoverflow.com/questions/2226386/python-vars-global-name-error) – manveti Jun 03 '19 at 22:04
  • You really should not be using `vars()` in this way. The normal Python solution to the need for dynamically-named variables is to use a `dict`. – BoarGules Jun 03 '19 at 23:56

1 Answers1

1

You're inside a function and therefore outside of the same namespace where vars() doesn't contain those variables. Have a look at this example:

x = 1
print('Outside', x, 'x' in vars())

def f():
    global x
    print('Inside', x, 'x' in vars())

f()

It prints:

Outside 1 True
Inside 1 False

As you can see, even though we have global x and can print its value, it's not a key in vars() inside the function.

Also: Why are global variables evil?

Why did you choose to use vars() in the first place? Maybe you could just use a separate dict object instead? There's a lot of information in this question Python: Using vars() to assign a string to a variable

An improved version of the example from above might look like this:

data = {}
key = 'x'
data[key] = 1

print('Outside', data['x'], 'x' in data)

def f(data):
    print('Inside', data['x'], 'x' in data)

f(data)

Of course, instead of 'x', you can use your own keys like str("var_cheeze_sum"+str(serie)).

finefoot
  • 9,914
  • 7
  • 59
  • 102
  • well i did this in my code to be sure if it worked where var_cheeze_value11 is a variable with id 1 and series 1 print("var11 =",var_cheeze_value11.get()) vars()[str("var_cheeze_sum"+str(serie))] += vars()[str("var_cheeze_value"+str(id-val+o)+str(serie))].get() and it gave me the correct value while the one after the += doesn't work – Julien Siefridt Jun 03 '19 at 22:41