问题描述
我将radiobutton放置在4×4矩阵中(使用循环)并在2维列表中保留
对它们的引用(rBtns [r] [c])。它的工作原理很好,我甚至可以通过将每列分配给一个intVar来使每个列只有一个按钮可以选择
。在许多语言中,
radiobutton都有一个属性可以直接读取,看看是否在未选中时选择了
。 Tkinter radiobuttons似乎没有任何
这样的财产。有没有办法看(通过脚本而不是屏幕)
来确定它是否被选中?或者这只能通过
控制变量来实现?
比尔
radiobutton的值和变量选项似乎就是你要找的东西。这是一个展示如何使用它们的例子:
---------------------------- ------------------------------------
$ t $ b来自Tkinter import *
root = Tk()
v = StringVar()
Radiobutton(root,text =''foo'',value =' 'foo'',variable = v).pack()
Radiobutton(root,text =''bar'',value =''bar'',variable = v).pack()
def p():
print v.get()
Button(root,command = p, text =''打印'')。pack()
root.mainloop()
------------- -------------------------------------------------- -
HTH
-
python -c" print''''。join([chr(154 - ord(c))''中的c(17zX(%,5.zmz5(17; 8(%,5.Z65 \''* 9--56l7 + - ''])"
radiobutton的值和变量选项似乎是你正在寻找的东西。
谢谢,我知道,但我正在寻找一种直接读取
选择/未选择状态的方法。由于rb矩阵的关系,使用控制变量得到一点点凌乱。它们是以4×4矩阵排列的,其中每列被分组(通过intVars)所以
每列不超过1 rb,但每行弥补
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
我一直在考虑的一个解决方案要求将每个rb的值设置为行数(0,1,2或3或1,2,3,或4)需要
使用0代表''none selected''),并为每列使用一个intVar。
然后我必须循环遍历所有四个intVars四次到
确定每行中选择的单选按钮。这就是我所谓的b $ b意味着凌乱。
比尔
Eric Brunel写道:周六, 2005年6月25日19:34:50 GMT,William Gill< no ***** @ gcgroup.net>写道:
radiobutton的值和变量选项似乎是你正在寻找的东西。这是一个展示如何使用它们的例子:
-------------------------------- --------------------------------
来自Tkinter进口*
root = Tk()
v = StringVar()
Radiobutton(root,text =''foo'',value =''foo'',variable = v).pack()
Radiobutton( root,text =''bar'',value =''bar'',variable = v).pack()
def p():
print v.get()
按钮(root,command = p,text ='''打印'')。pack()
root.mainloop()
------ -------------------------------------------------- --------
HTH
您可以写一个小帮手功能
def def(rbn):
返回rbn.getvar(rbn [" variable" ;])== rbn [" value"]
或使用Tkinter.Radiobutton的自定义子类,并带有''selected''属性:
class Radiobutton(Tkinter.Radiobutton):
def __getattr __(self,name):
if name ==" selected":
返回self.getvar(self [" variable"])== self [" value"]
raise AttributeError
Peter
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep
references to them in a 2 dimensional list ( rBtns[r][c] ). It works
fine, and I can even make it so only one button per column can be
selected, by assigning each column to an intVar. In many languages a
radiobutton has a property that can be directly read to see if it is
selected on unselected. Tkinter radiobuttons don''t seem to have any
such property. Is there any way to look (via the script not the screen)
to determine if it is selected?, or can this only be achieved via
control variables?
Bill
The value and variable options for a radiobutton seem to be what you''re looking for. Here is an example showing how to use them:
----------------------------------------------------------------
from Tkinter import *
root = Tk()
v = StringVar()
Radiobutton(root, text=''foo'', value=''foo'', variable=v).pack()
Radiobutton(root, text=''bar'', value=''bar'', variable=v).pack()
def p():
print v.get()
Button(root, command=p, text=''Print'').pack()
root.mainloop()
----------------------------------------------------------------
HTH
--
python -c "print ''''.join([chr(154 - ord(c)) for c in ''U(17zX(%,5.zmz5(17;8(%,5.Z65\''*9--56l7+-''])"
The value and variable options for a radiobutton seem to be what
you''re looking for.
Thanks, I knew that, but I was looking for a way to read the
selected/unselected status directly. Using control variables gets a
little messy because of the relationship of the rb matrix. They are
arranged in a 4 X 4 matrix where each column is grouped (via intVars) so
that no more than 1 rb per column can be selected, but each row makes up
the ''status'' on one ''item'' so any combination of buttons in a row is
acceptable.
One solution I have been contemplating requires setting the value of
each rb to the row number ( 0, 1, 2, or 3 or 1,2,3, or 4 in case I need
to use 0 for ''none selected''), and using one intVar for each column.
Then I would have to loop through all four intVars four times to
determine which radiobuttons are selected in each row. That''s what I
mean by messy.
Bill
Eric Brunel wrote: On Sat, 25 Jun 2005 19:34:50 GMT, William Gill <no*****@gcgroup.net> wrote:
The value and variable options for a radiobutton seem to be what you''re
looking for. Here is an example showing how to use them:
----------------------------------------------------------------
from Tkinter import *
root = Tk()
v = StringVar()
Radiobutton(root, text=''foo'', value=''foo'', variable=v).pack()
Radiobutton(root, text=''bar'', value=''bar'', variable=v).pack()
def p():
print v.get()
Button(root, command=p, text=''Print'').pack()
root.mainloop()
----------------------------------------------------------------
HTH
You can either write a little helper function
def selected(rbn):
return rbn.getvar(rbn["variable"]) == rbn["value"]
or use a custom subclass of Tkinter.Radiobutton with a ''selected'' attribute:
class Radiobutton(Tkinter.Radiobutton):
def __getattr__(self, name):
if name == "selected":
return self.getvar(self["variable"]) == self["value"]
raise AttributeError
Peter
这篇关于tkinter radiobutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!