请问怎么实现Radio的选中事件
Hi 各位达人:
请看我的代码,我想鼠标点击Radiobutton的时候,可以把选择的button通过回调的方式在selected的方法中打印出来? 现在回调时没有问题,但是我怎么知道那个是被选中的那?
还是Tk的时间模型不是这样的,应该用bind方法?但是我看doc里面Radiobutton没有bind的方法啊?还有问一下Tk里面不断的用TopLevel是为什么,我不太理解
代码如下:
#Driver examination
from Tkinter import *
class Question:
def __init__(self, title, answer, linearray):
"""Create new Question. linearray is question line array object. """
self.title = title
self.answer = answer
self.linearray = linearray
def isright(self, answer):
"""Inpute answer is right, 'A', 'B', 'C"""
return self.answer==answer
class QuestionPanel(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.top = master
# self.question = question
def validateAnswer(self, question):
""" Validate input answere is right"""
Label(self.top, text="You're right!!").grid(row=(question.linearray.__len__))
def selected(self, event=None):
print event
def randerQuestion(self, question):
"""Display spec quesiton on Window"""
# font = Font(size=12)
Label(self.top, text=question.title).pack(anchor=W)
count =1
v = IntVar()
for line in question.linearray:
Radiobutton(self.top, text=line, variable=v, value = line[0], command=self.selected).pack(anchor=W)
# count = count+1
question = Question("What's your age?","C",['A, 28','B, 29','C, 30'])
root = Tk()
#v = IntVar()
#for x in question.linearray:
# Radiobutton(root, text=x, variable = v, value=x[0]).pack(anchor=W)
questionPanel = QuestionPanel(root)
questionPanel.randerQuestion(question)
#QuestionPanel.validateAnswer(question)
#print "Size:", question.linearray.index
root.mainloop()
#for x in question.linearray:
# print x
#print 'Pls select one answer'
#print question.isright('A')