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

mem_fox mem_fox 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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')
...全文

相关推荐

layui中使用jquery控制radio选中事件的示例代码

今天小编就为大家分享一篇layui中使用jquery控制radio选中事件的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

weixin_38705723

VC单选按钮控件(Radio Button)用法

先为对话框加上2个radio button,分别是Radio1和Radio2。 问题1:如何让Radio1或者Radio2默认选上?如何知道哪个被选上了? 关键是选上,“默认”只要放在OnInitDialog()即可。三种方法可以让它选上, 第一种: ((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);//选上 ((CBu

lanyanjiayu

微信小程序之自定义Radio样式

wxml <radio-group bindchange="radioChange" class="radio-group-width"> <label class="radio-label" wx:if="{{rechargeValue.type=='关台'}}"> <view class="radio-text">余额</view> <radio value="1" ></radio> ...

liuye066

radio change事件监听全攻略:从原理到实战,避开所有坑

在网页表单开发中,radio 单选按钮是高频使用的控件,而如何正确监听其选中状态变化,是许多前端初学者甚至老手都会踩坑的细节。与 click 事件不同,change 事件严格遵循“值发生变化才触发”的语义,因此在 radio 上它成为标准的事件监听方式。理解 name 分组带来的互斥与提交规则,是掌握监听逻辑的前提。绑定事件时,内联 onchange 虽然简单却不利于维护,addEventListener 适合静态表单,而事件委托则天然支持动态渲染的选项,是复杂页面的首选方案。基于事件对象、checked

戴文渊

jQuery实现radio第一次点击选中第二次点击取消功能

本篇文章主要介绍了jQuery实现radio第一次点击选中第二次点击取消功能的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧

weixin_38587473
评论 3
收藏 已收藏
分享
返回顶部
mem_fox
脚本语言 37,738 成员 34,208 内容