tkinter 单选按钮在函数内部不起作用
我正在尝试编写一个带有单选按钮的 GUI(实际上是一个 PyMOL 插件)。我需要将单选按钮放在函数内。我有两个问题(如果单选按钮位于功能之外,我没有任何问题):
1)最后两个单选按钮看起来是灰色的并已选中。 (我认为正确的行为应该是只有一个被选中,没有灰色)设置不同的默认值(使用 ref_value.set())不会改变任何东西
2)当点击时我总是得到默认值(本例中为“1”)提交按钮。
from Tkinter import *
def __init__(self):
"""this adds the Plugin to the PyMOL menu"""
self.menuBar.addmenuitem('Plugin', 'command',
'Plugin name',
label = 'plugin',
command = lambda : draw_gui())
def draw_gui():
global v
master = Tk()
master.title(' title ')
Button(master, text='Submit', command=submit).pack(side=BOTTOM)
v = StringVar()
v.set(1)
Radiobutton(master, text='option 1', variable=v, value=1).pack(side=LEFT)
Radiobutton(master, text='option 2', variable=v, value=2).pack(side=LEFT)
Radiobutton(master, text='option 3', variable=v, value=3).pack(side=LEFT)
master.mainloop()
def submit():
print v.get()
提前致谢
I am trying to write a GUI (in fact a PyMOL plugin) with radio buttons. I need the radio buttons to be inside a function. I have two problems (if the radio buttons are outside a function I don´t have any problem):
1) the last two radio button looks gray and checked. (I think the correct behavior should be only one checked and none gray) setting a different default value (using ref_value.set()) does not change anything
2) I always get the default value ("1" in this example) when hit the submit button.
from Tkinter import *
def __init__(self):
"""this adds the Plugin to the PyMOL menu"""
self.menuBar.addmenuitem('Plugin', 'command',
'Plugin name',
label = 'plugin',
command = lambda : draw_gui())
def draw_gui():
global v
master = Tk()
master.title(' title ')
Button(master, text='Submit', command=submit).pack(side=BOTTOM)
v = StringVar()
v.set(1)
Radiobutton(master, text='option 1', variable=v, value=1).pack(side=LEFT)
Radiobutton(master, text='option 2', variable=v, value=2).pack(side=LEFT)
Radiobutton(master, text='option 3', variable=v, value=3).pack(side=LEFT)
master.mainloop()
def submit():
print v.get()
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我应该明确设置主控,否则它将使用 PyMOL GUI 作为主控。
PS:感谢托马斯·霍尔德的回答。
I should set the master explicitly, otherwise it will use the PyMOL GUI as master.
PS: thanks Thomas Holder for the answer.
它对我有用(我复制了您的代码并添加了行
from Tkinter import *
和if __name__ == "__main__": draw_gui()
我认为您也有) 。所有单选按钮都有效,我在输出中得到适当的 1、2、3。所以如果你运行同样的代码,那么环境就有问题。我在 Ubuntu 10.04 上有 Tkinter 73770 和 Python 2.6.5,并且正在从命令行运行脚本。
您是否尝试从空闲状态运行它?这通常与使用 Tkinter 的其他脚本配合不好,因为它是用 Tkinter 本身编写的。如果是这样,请尝试从命令行运行。
It works for me (I copied your code and added the lines
from Tkinter import *
andif __name__ == "__main__": draw_gui()
which I assume you have too). All the radio buttons work and I get 1, 2, 3 as appropriate in the output.So if you are running the same code, there is a problem with the environment. I have Tkinter 73770 and Python 2.6.5 on Ubuntu 10.04 and am running the script from the command line.
Are you perhaps trying to run it from IDLE? That often doesn't play nice with other scripts that use Tkinter, because it is written in Tkinter itself. If so, try running from the command line instead.