请问怎么实现Radio的选中事件

mem_fox 2006-08-09 05:06:24
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')
...全文
465 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2006-10-17
  • 打赏
  • 举报
回复
不是十分了解,但是很容易通过稍微变通的方法来解决。

为之声明一个事件类:


class NamedEvent:
def __init__(self, selected):
self.name = selected
def __call__(self):
print self.name

然后把下面的代码:

Radiobutton(self.top, text=line, variable=v, value = line[0], command=self.selected).pack(anchor=W)

中的command=self.selected换成:

command=NamedEvent(line[0])

即:

Radiobutton(self.top, text=line, variable=v, value = line[0], command=NamedEvent(line[0])).pack(anchor=W)

其中line[0]是你想打印的东西。

现在你按到第一个选项的时候就会打印A。

mem_fox 2006-10-17
  • 打赏
  • 举报
回复
用python的朋友们帮忙啊
ChumpKlutz 2006-08-10
  • 打赏
  • 举报
回复
帮你顶了

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧