Python Forum

Full Version: Can't Get Entry Var in Tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
class Classe1():

    def button_label(self, chosen_row, textoescrito, variavel):
        lb1 = Label(frame2, text=textoescrito, bg=color2, font=('Roboto', 11))
        lb1.grid(row=chosen_row, column=2, padx=2, pady=2)
        self = Entry(frame2, width=50, font=('Roboto', 11), textvariable=variavel)
        self.grid(row=chosen_row + 1, column=2, padx=2, pady=2)

    def button_label2(self, chosen_row, textoescrito, variavel):
        lb1 = Label(frame2, text=textoescrito, bg=color2, font=('Roboto', 11))
        lb1.grid(row=chosen_row, column=3, padx=2, pady=2)
        self = Entry(frame2, width=50, font=('Roboto', 11), textvariable=variavel)
        self.grid(row=chosen_row + 1, column=3, padx=2, pady=2)

    def get1(self):
        self = StringVar.get()


text1 = Classe1()
text1.button_label(3, 'Razão Social:', 'razao')
text2 = Classe1()
text2.button_label(5, 'Nome Fantasia:', 'nomefantasia')
text3 = Classe1()
text3.button_label(7, 'Endereço:', 'endereco')
text4 = Classe1()
text4.button_label(9, 'Responsável Legal:', 'res_legal')
text5 = Classe1()
text5.button_label(11, 'Telefone:', 'telefone')
text6 = Classe1()
text6.button_label(13, 'CNPJ:', 'cnpj')
text7 = Classe1()
text7.button_label2(3, 'Data:', 'data')
text8 = Classe1()
text8.button_label2(5, 'Email:', 'email')
text9 = Classe1()
text9.button_label2(7, 'Horário de funcionamento:', 'horario')
text10 = Classe1()
text10.button_label2(9, 'Área total(m3):', 'areatotal')
text11 = Classe1()
text11.button_label2(11, 'Cpf do responsável legal', 'cpf1')
text12 = Classe1()
text12.button_label2(13, 'Responsável pela implantação:', 'res_imp1')

app.mainloop()
When I try to use 'razao' etc, it gives me this code:
Error:
Exception in Tkinter callback Traceback (most recent call last): File "c:\users\joaog\appdata\local\programs\python\python38-32\Lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "GUI.py", line 21, in clicked doc.add_paragraph(res_legal, style='Heading 1') NameError: name 'res_legal' is not defined
textvariable must be a tkinter.StringVar, not a raw string.

you can remedy this by declaring a string var in button_label, seting it from the argument variavel
and using the new StringVar as the textvariable (cod not tested):
class Classe1():
    def __init__(self):
        self.svar = StringVar()

    def button_label(self, chosen_row, textoescrito, variavel):
        self.svar.set(variavel)
        lb1 = Label(frame2, text=textoescrito, bg=color2, font=('Roboto', 11))
        lb1.grid(row=chosen_row, column=2, padx=2, pady=2)
        self = Entry(frame2, width=50, font=('Roboto', 11), textvariable=self.svar)
        self.grid(row=chosen_row + 1, column=2, padx=2, pady=2)