tkinter, method .get does not return anything

0

The problem is that although I write something in the Entry fields, and I link them correctly to a variable, when evaluating them with the .get and see what they got, it returns null.

   ventana_añadir = Tk()
    ventana_añadir.title("Añadir un Usuario")
    ventana_añadir.geometry("550x200+600+250")
    ventana_añadir.resizable(width=False, height=False)
    nombre = StringVar()
    apellido = StringVar()
    id = StringVar()
    edad = StringVar()
    salario = StringVar()
    seleccion = Label(ventana_añadir, text = "Añadir Nueva Entrada", font = ("Times", 25)).place(x = 100, y = 10)
    Nombre_Label = Label(ventana_añadir, text = "Nombre ").place(x= 39, y = 70)
    Apellido_Label = Label(ventana_añadir, text = "Apellidos ").place(x = 239, y = 70)
    Edad_Label= Label(ventana_añadir, text = "Edad").place(x = 37, y = 110)
    Salario_Label = Label(ventana_añadir, text = "Salario").place(x = 100, y = 110)
    Nombre_Entry = Entry(ventana_añadir, textvariable = nombre, width = 30).place(x = 40, y = 90)
    Apellido_Entry = Entry(ventana_añadir, textvariable = apellido, width = 45).place(x = 240, y = 90)
    Edad_Spinbox = Spinbox(ventana_añadir, textvariable = edad, from_ = 10 , to = 99, state = "readonly", width = 5).place(x = 40, y = 130)
    Salario_Entry = Entry(ventana_añadir, textvariable = salario, width = 15).place(x = 100, y = 130)
    Añadir_Button = Button(ventana_añadir, text = "Añadir", font = ("Helvetica", 18),command = lambda: verificar_datos(str(nombre.get()), str(apellido.get()), str(edad.get()), str(salario.get()))).place(x = 240 , y = 120)
    ID_Label = Label(ventana_añadir, text = "ID").place(x = 39, y = 150 )
    ID_Entry = Entry(ventana_añadir, textvariable = id, state = "readonly").place(x = 40, y = 170)
    
asked by Cesar Cuevas 27.12.2016 в 01:35
source

2 answers

0

I've tried your code, since I do not know what the verificar_datos function does, I've made it print the data:

from tkinter import *

def verificar_datos(nom, apell, ed, sal):
    print(nom)
    print(apell)
    print(ed)
    print(sal)

ventana_añadir = Tk()
ventana_añadir.title("Añadir un Usuario")
ventana_añadir.geometry("550x200+600+250")
ventana_añadir.resizable(width=False, height=False)
nombre = StringVar()
apellido = StringVar()
id = StringVar()
edad = StringVar()
salario = StringVar()
seleccion = Label(ventana_añadir, text="Añadir Nueva Entrada", font=("Times", 25)).place(x=100, y=10)
Nombre_Label = Label(ventana_añadir, text="Nombre ").place(x=39, y=70)
Apellido_Label = Label(ventana_añadir, text="Apellidos ").place(x=239, y=70)
Edad_Label = Label(ventana_añadir, text="Edad").place(x=37, y=110)
Salario_Label = Label(ventana_añadir, text="Salario").place(x=100, y=110)
Nombre_Entry = Entry(ventana_añadir, textvariable=nombre, width=30).place(x=40, y=90)
Apellido_Entry = Entry(ventana_añadir, textvariable=apellido, width=45).place(x=240, y=90)
Edad_Spinbox = Spinbox(ventana_añadir, textvariable=edad, from_=10, to=99, state="readonly", width=5).place(x=40, y=130)
Salario_Entry = Entry(ventana_añadir, textvariable=salario, width=15).place(x=100, y=130)
Añadir_Button = Button(ventana_añadir, text="Añadir", font=("Helvetica", 18),
                       command=lambda: verificar_datos(nombre.get(), apellido.get(), str(edad.get()),
                                                       str(salario.get()))).place(x=240, y=120)
ID_Label = Label(ventana_añadir, text="ID").place(x=39, y=150)
ID_Entry = Entry(ventana_añadir, textvariable=id, state="readonly").place(x=40, y=170)
ventana_añadir.mainloop()

And it works for me:

Exit:

nombre
Apellidos
14
1000.0

Furthermore, it is not necessary to pass the values of the variables to the function since the StringVar are global variables.

from tkinter import *


def verificar_datos():
    print(nombre.get())
    print(apellido.get())
    print(edad.get())
    print(salario.get())

ventana_añadir = Tk()
ventana_añadir.title("Añadir un Usuario")
ventana_añadir.geometry("550x200+600+250")
ventana_añadir.resizable(width=False, height=False)
nombre = StringVar()
apellido = StringVar()
id = StringVar()
edad = StringVar()
salario = StringVar()
seleccion = Label(ventana_añadir, text="Añadir Nueva Entrada", font=("Times", 25)).place(x=100, y=10)
Nombre_Label = Label(ventana_añadir, text="Nombre ").place(x=39, y=70)
Apellido_Label = Label(ventana_añadir, text="Apellidos ").place(x=239, y=70)
Edad_Label = Label(ventana_añadir, text="Edad").place(x=37, y=110)
Salario_Label = Label(ventana_añadir, text="Salario").place(x=100, y=110)
Nombre_Entry = Entry(ventana_añadir, textvariable=nombre, width=30).place(x=40, y=90)
Apellido_Entry = Entry(ventana_añadir, textvariable=apellido, width=45).place(x=240, y=90)
Edad_Spinbox = Spinbox(ventana_añadir, textvariable=edad, from_=10, to=99, state="readonly", width=5).place(x=40, y=130)
Salario_Entry = Entry(ventana_añadir, textvariable=salario, width=15).place(x=100, y=130)
Añadir_Button = Button(ventana_añadir, text="Añadir", font=("Helvetica", 18),
                       command=verificar_datos).place(x=240, y=120)
ID_Label = Label(ventana_añadir, text="ID").place(x=39, y=150)
ID_Entry = Entry(ventana_añadir, textvariable=id, state="readonly").place(x=40, y=170)
ventana_añadir.mainloop()
    
answered by 27.12.2016 в 01:51
0

I already solved it, if someone has the same problem, what should be done is to specify within StringVar () the window over which the variables will be created.

example

window = Tk () var = StringVar (window)

    
answered by 30.12.2016 в 08:46