Reputation: 31
I'm trying to create an app in python. Most of the times, all radio buttons except the first one is already selected, and the first one selected after hovering. Please note that this happens 9 out of 10 times, and once it works as intended.
Code is included below. EDIT: Code repasted. I think it went off during copy-paste. Sorry!
import sys
from Tkinter import *
i = 0
#for i in range(0, 10):
# print i
macro_sheet_names = [1, 2, 3, 4]
print len(macro_sheet_names)
root = Tk()
var = IntVar()
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection)
print ('Tab selected: ' + str(var.get()))
root.destroy()
# sys.exit()
# root.withdraw()
i = 0
for i in range (0, len(macro_sheet_names)):
R = Radiobutton(root, text = macro_sheet_names[i], variable = var, value = i, command = sel)
R.pack(anchor = W)
label = Label(root)
label.pack()
root.mainloop()
print ('Exiting...')
sys.exit()
Upvotes: 0
Views: 500
Reputation: 1279
For me it works on Python 3 with modification. Try this:
import sys
from Tkinter import *
i = 0
#for i in range(0, 10):
# print i
macro_sheet_names = [1, 2, 3, 4]
print (len(macro_sheet_names))
root = Tk()
var = IntVar()
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection)
print ('Tab selected: ' + str(var.get()))
root.destroy()
# sys.exit()
# root.withdraw()
i = 0
for i in range (0, len(macro_sheet_names)):
R = Radiobutton(root, text = macro_sheet_names[i], variable = var, value = i, command = sel)
if i == 0: R.select ()
R.pack(anchor = W)
label = Label(root)
label.pack()
root.mainloop()
print ('Exiting...')
sys.exit()
It simply selects the first radio-box automatically.
Upvotes: 1