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