我正在尝试在Python中创建一个应用。在大多数情况下,除第一个按钮外,所有无线电按钮都已选择,并且在悬停后选择了第一个按钮。请注意,这发生在10分之9中,一旦按预期工作。
代码如下。编辑:代码已备份。我认为在复制过程中它发生了。抱歉!
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()
对我来说,它可以在python 3上进行修改。尝试以下操作:
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()
它只会自动选择第一个播放框。