0

from the List of radio button I want to know which one was clicked

Whenever a radio button (In python Tkinter) is clicked its returning 0...

I tried the following method:

  • declaring the 'var' variable global
  • passing var variable in all function

But none of the steps are working

def get_date(var):
    path_read = E1.get()
    date_list = readunparseddata.getdate_unparseddate(path_read)
    show_date(date_list,var)


def show_date(list_date,var):
    print(var)
    frame = Tk()
    #v.set(1)
    Label(frame,text="""Choose your Date :""",justify=LEFT,padx=20).pack( anchor = W )
    count = 0
    for date in list_date:
        print count
        R1=Radiobutton(frame, text=date, padx=20, value=count, variable=var, command=lambda:ShowChoice(var))
        R1.pack()
        count+=1

def ShowChoice(var):
    print "option : " + str(var.get())


top = Tk()
var=IntVar()

2 Answers2

1

The problem was with the instance of Tk() that I was creating. Below link ( 1 ) said to use TopLevel() which solved the problem

  • [link](https://stackoverflow.com/questions/35010905/intvar-returning-only-0-even-with-get-function?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) The link which i referenced – Avinash Agarwal Apr 16 '18 at 12:53
0

Increment the counter in the function that is being called when the radio button is selected. Here is an example to help you.It prints the number of times the button is selected.

import Tkinter as tk
count=0
root = tk.Tk()
def add():
    global count
    count=count+1
    print count
v = tk.IntVar()

tk.Label(root, 
        text="""Choose a 
programming language:""",
        justify = tk.LEFT,
        padx = 20).pack()
tk.Radiobutton(root, 
              text="Python",
              padx = 20, 
              variable=v, 
              value=1,command=add).pack(anchor=tk.W)
root.mainloop()
Sr7
  • 86
  • 1
  • 9
  • I don't want a counter to calculate the number of times a radio button is clicked, rather know from a list of radio button which one is clicked. – Avinash Agarwal Apr 16 '18 at 11:45