How to update values contained in Radiobutton to use them in a Python Tkinter function?

1

I am trying to place three options using the Radiobutton widget to later use the value of the option selected in a function when a button is pressed.

I have found scripts where a variable of type IntVar is defined and then the method .get() is invoked to obtain the value of these Radiobuttons , however my problem is not solved.

The function that is called when you press the button is:

def presiona_consultar(_mes,_year,_tr):
    print _mes
    print _year
    print _tr

The part that defines the radiobutton is:

tr = IntVar()
Radiobutton(lffecha, text="Anual", variable=tr , value= 1).place(x=10 , y=5)
Radiobutton(lffecha, text="Mensual", variable=tr , value= 2).place(x=110 , y=5)
Radiobutton(lffecha, text="Actual", variable=tr , value= 3).place(x=230 , y=5)

and the Button is:

consultar = Button(inicio,text='Reporte',command= lambda : presiona_consultar(_mes=mes.get(),_year=year.get(),_tr= tr.get() )
consultar.place(x=535,y=600)

Pressing "report" correctly prints the month and the year (_mes, _year) which I programmed with a Spinbox widget, but when printing the type (_tr) it only prints the value 0, that is, the .get () does not get the values of the radiobuttons Annual = 1, Monthly = 2 or Actual = 3

My parent window is inicio=Tk() and I have a LabelFrame which calls lffecha which groups the options of the 3 Radiobuttons and the Spinboxes mentioned above. Next the code that represents a module of my app

from Tkinter import *
from valida import *


def presiona_salir(inicio):
    "VALIDA LA OPCION Yes/No"
    valida_salida         #FUNCION CONTENIDA EN EL MODULO valida
    if valida_salida() == True:
        print 'Saliste'
        inicio.destroy()
    else:
        print "Te quedaste"
    return #Funcion Sin Problemas !!


def presiona_consultar(_mes,_year,_tr):
    print _mes
    print _year
    print _tr


def inicio(user):
    "CREA LA PANTALLA DE GENERADOR DE REPORTE"
    inicio = Tk()
    inicio.title("INICIO")
    inicio.config(bg="gray")
    inicio.geometry("1280x670+25+20")
    inicio.resizable(width=False, height=False)  
    _user=(str("Usuario: ") )+ str(user.upper()).replace("."," ")
    saluda = Label(inicio, text = _user, bg="gray", font = ("Century Gothic",20))
    saluda.place(x=5,y=5)

    lffecha=LabelFrame(inicio,text='Consulta',bg="gray",labelanchor='n', height=300,width=340, font = ("Century Gothic",15))
    lffecha.place(x=10,y=75)

    tr = IntVar(value = 2)
    Radiobutton(lffecha, text="Anual", variable=tr , value= 1 , font = ("Century Gothic",14), bg="gray").place(x=10 , y=5)
    Radiobutton(lffecha, text="Mensual", variable=tr , value= 2 , font = ("Century Gothic",14), bg="gray").place(x=110 , y=5)
    Radiobutton(lffecha, text="Actual", variable=tr , value= 3 , font = ("Century Gothic",14), bg="gray").place(x=230 , y=5)

    mes=Spinbox(lffecha, values=('Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'),wrap=True,width=12,font = ("Century Gothic",20),justify=CENTER)
    mes.place(x=7 , y=50)

    year=Spinbox(lffecha,from_=2012,to=2017,incremen=1,wrap=True,font = ("Century Gothic",20),width=6,justify=CENTER)
    year.place(x=215,y=50)

    consultar = Button(inicio,text='GENERAR REPORTE',font = ("Century Gothic",20) ,command= lambda : presiona_consultar(_mes=mes.get(),_year=year.get(),_tr=tr.get() ))
    consultar.place(x=535,y=600)

    salir = Button(inicio,text='Salir',font = ("Clarendon Light",15), relief=FLAT, bg="gray", fg="RED" , command=lambda       inicio=inicio:presiona_salir(inicio))
    salir.place(x=1210,y=10)

    mainloop()
    
asked by Javier 07.03.2017 в 20:50
source

2 answers

1

Problem solved, it was only necessary to place the labelframe inside the containers of variable tr. The code is solved by replacing the expression tr = IntVar() with tr = IntVar(lffecha) . Greetings

    
answered by 09.03.2017 в 14:47
0

You should not have any problem in appearance, using your code as a base we can reconstruct a reproducible code that works correctly:

from Tkinter import *

def presiona_consultar(_mes,_year,_tr):
    print _mes
    print _year
    print _tr

root = Tk()
root.geometry("300x200")

mes = StringVar(value='Marzo')
year = StringVar(value='2017')
tr = IntVar(value = 2)

Radiobutton(root, text="Anual", variable=tr, value=1).place(x=10 , y=5)
Radiobutton(root, text="Mensual", variable=tr , value= 2).place(x=110 , y=5)
Radiobutton(root, text="Actual", variable=tr , value= 3).place(x=230 , y=5)

consultar = Button(root,text='Reporte',command= lambda : presiona_consultar(_mes=mes.get(),_year=year.get(),_tr= tr.get() ))
consultar.place(x=120,y=100)

root.mainloop()

By default no radiobutton will be selected and the variable has a value of 0, if one is selected the value changes correctly. In my example, the variable with a value (2) is initialized, which means that when the app starts, the corresponding radiobutton ( Mensual ) is selected by default.

If the problem is not in this there should be some information that escapes us as% are lffecha e inicio (frames, secondary windows, dialogs, etc), which is the parent of the variables, etc. It would be good if you edited the question adding a minimum example that shows your problem but functional so that we can reproduce the error and see if we can detect the problem.

    
answered by 07.03.2017 в 22:02