Python Tkinter 中的单选按钮值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35660342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Radio button values in Python Tkinter
提问by Bird
I am trying to create a dialog box in Python using Tkinter. The goal is to have a dialog box with two radio buttons and an "OK" button. Radio button one selects the option "default". Radio button two selects the option "user defined." The "OK" button closes the window.
我正在尝试使用 Tkinter 在 Python 中创建一个对话框。目标是有一个带有两个单选按钮和一个“确定”按钮的对话框。单选按钮一选择选项“默认”。单选按钮二选择“用户定义”选项。“确定”按钮关闭窗口。
Question 1:How do I save the value from the radio button? That is, how do I pass the selected radio button to the rest of my script?
问题 1:如何保存单选按钮的值?也就是说,如何将选定的单选按钮传递给脚本的其余部分?
Question 2:How can I have the second radio button include user text input (along the lines of tkSimpleDialog.askstring)? I would like that button to show a radiobutton, a prompt ("Enter value:"), and a space for the user to enter text -- all on one line as a single radiobutton option.
So the whole dialog should have the top radio button be a normal radio button, and the second button specify user input and include a space for that user input (and the OK button).
问题 2:如何让第二个单选按钮包含用户文本输入(沿着 tkSimpleDialog.askstring 的行)?我希望该按钮显示一个单选按钮、一个提示(“输入值:”)和一个供用户输入文本的空间——所有这些都在一行中作为一个单选按钮选项。
所以整个对话框的顶部单选按钮应该是一个普通的单选按钮,第二个按钮指定用户输入并为该用户输入(和确定按钮)包含一个空格。
So far I have a dialog open with two options, but the value doesn't get passed to anything I can see; selection is returned as 0 even before I select a radiobutton.
Any help on either question would be greatly appreciated, thank you.
Here's my script so far:
到目前为止,我打开了一个带有两个选项的对话框,但该值没有传递给我能看到的任何内容;即使在我选择一个单选按钮之前,selection 也会返回为 0。
对任何一个问题的任何帮助将不胜感激,谢谢。
到目前为止,这是我的脚本:
from Tkinter import*
master = Tk()
var = IntVar()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command = master.quit).grid(row=3, sticky=W)
selection = var.get()
print "Selection:", selection
mainloop()
#If selection == 0 do one thing
#If selection == 1 do something else...
采纳答案by JiyuuSensei
A bit late to the party, but I stumbled upon this question while trying to find something on Tkinter radiobuttons.
参加聚会有点晚了,但我在尝试在 Tkinter 单选按钮上找到一些东西时偶然发现了这个问题。
Question 1:
问题 1:
I changed three things:
我改变了三件事:
1) I immediately set the value of var
to 1
after you've defined it. This is done by doing var.set(1)
and will make sure your first radio button is selected (which has a value of 1, as you defined it later on in the code).
1)我立即设置的值var
给1
你定义后。这是通过这样做来完成的var.set(1)
,并将确保您的第一个单选按钮被选中(其值为 1,正如您稍后在代码中定义的那样)。
2) I've replaced your master.quit
command with a function called quit_loop
. In this function:
2)我已经master.quit
用一个名为quit_loop
. 在这个函数中:
- The
var
value is printed through aprint
andget
statement. Theget
will 'get' the current value ofvar
, which depends on which radio button is selected. - I create a global variable within this function, which will then
get
the current value ofvar
. - I added parentheses to
master.quit()
because this is no longer in the command of a radio button. Note that if you plan on using IDLE,master.destroy()
might be a more suitable alternative.
- 该
var
值通过print
andget
语句打印。该get
会“得到”的当前值var
,这取决于哪个单选按钮被选中的。 - 我在这个函数中创建了一个全局变量,然后
get
将var
. - 我添加了括号,
master.quit()
因为这不再是单选按钮的命令。请注意,如果您打算使用 IDLE,master.destroy()
可能是更合适的替代方案。
3) Due to the creation of the selection
variable in the function we now have your wanted value stored in a variable. There is one final if
-statement at the end of the code to show it's working.
3) 由于selection
在函数中创建了变量,我们现在将您想要的值存储在变量中。if
代码末尾有一个最终语句来表明它正在工作。
from Tkinter import *
master = Tk()
var = IntVar()
var.set(1)
def quit_loop():
print "Selection:",var.get()
global selection
selection = var.get()
master.quit()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable=var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable=var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command=quit_loop).grid(row=3, sticky=W)
master.mainloop()
if selection == 1:
print "My Value is equal to one."
elif selection == 2:
print "My value is equal to two."
Question 2:
问题2:
I would keep it simple and just add a label and an entry box right after your radio button. This means that we also have to work with columns as you didn't have any defined in your previous code, which makes everything default to column 0. We want your second radio button to be 'radio, label, entry' which takes three columns.
我会保持简单,只需在单选按钮后添加一个标签和一个输入框。这意味着我们还必须处理列,因为您在之前的代码中没有定义任何列,这使得所有内容都默认为列 0。我们希望您的第二个单选按钮是“radio、label、entry”,它需要三列.
1) The previous label containing "Select OCR language"
will be spanned over three columns with columnspan=3
added to the grid arguments. The same goes for your first radio button.
1) 先前包含的标签"Select OCR language"
将跨越三列,并columnspan=3
添加到网格参数中。您的第一个单选按钮也是如此。
2) I added a Label
and an Entry
after your second radio button. Note that the columns go from 0 to 2, defining our three columns. The label has a simple "Enter value:"
text, whereas the entry has the variable textvariable=entry_text
. I added this variable entry_text
to the beginning of your code and immediately set its value to ###
. Note that this is a string
(hence, textvariable) so adding checks for integer numbers only is up to you.
2)我在你的第二个单选按钮之后添加了一个Label
和一个Entry
。请注意,列从 0 到 2,定义了我们的三列。标签有一个简单的"Enter value:"
文本,而条目有变量textvariable=entry_text
。我将此变量添加entry_text
到代码的开头并立即将其值设置为###
. 请注意,这是一个string
(因此是文本变量),因此仅添加对整数的检查取决于您。
3) Of course, this is not linked to the second radio button. It still has a value of 2 if we select it, not the value of the Entry
widget. That's why, in the previously created quit_loop
function, I added a small if
statement that assigns the value of the entry to selection
if the second radio button was selected.
3) 当然,这与第二个单选按钮无关。如果我们选择它,它的值仍然是 2,而不是Entry
小部件的值。这就是为什么在之前创建的quit_loop
函数中,我添加了一个小if
语句,selection
如果选择了第二个单选按钮,该语句将条目的值分配给。
from Tkinter import *
master = Tk()
var = IntVar()
var.set(1)
entry_text = StringVar()
entry_text.set("###")
def quit_loop():
print "Selection:",var.get()
global selection
selection = var.get()
if selection == 2:
selection = entry_text.get()
master.quit()
# Add columnspan to these widgets
Label(master, text = "Select OCR language").grid(row=0, sticky=W, columnspan=3)
Radiobutton(master, text = "default", variable=var, value = 1).grid(row=1, sticky=W, columnspan=3)
# Order these widgets in their appropriate columns
Radiobutton(master, variable=var, value = 2).grid(row=2, sticky=W, column=0)
Label(master, text="Enter value:").grid(row=2, sticky=W, column=1)
Entry(master, textvariable=entry_text).grid(row=2, sticky=W, column=2)
# Example of what happens without columnspan
Button(master, text = "OK", command=quit_loop).grid(row=3, sticky=W)
master.mainloop()
print selection
Tip
提示
If this simple GUI remains this small, it's ok to write code in this manner. However, expanding a lot on this further I would suggest taking an object oriented approachas it really improves readability a lot, especially when functions are being defined. That way they don't have to be necessarily defined beforehand.
如果这个简单的 GUI 仍然如此小,那么以这种方式编写代码是可以的。然而,在这方面进一步扩展我建议采用面向对象的方法,因为它确实大大提高了可读性,尤其是在定义函数时。这样就不必事先定义它们。
回答by Tadhg McDonald-Jensen
Instead of directly using master.quit
in the Button's command, define a function that finishes up the program then calls master.quit()
:
不是直接master.quit
在 Button 的命令中使用,而是定义一个函数来完成程序然后调用master.quit()
:
def end_program(event=None):#event will let it be used as .bind callbacks too if you need it.
selection = var.get()
if selection:
NotImplemented
else:
NotImplemented
master.quit()
...
Button(master, text = "OK", command = end_program).grid(row=3, sticky=W)
one the master is closed some of the data from the widgets is cleaned up so master.quit()
needs to be called only after you are done accessing the widgets.
一个 master 被关闭,一些来自小部件的数据被清理,因此master.quit()
只有在您完成访问小部件后才需要调用。
回答by Kenly
As set the selection value will be set before the window appears (selection = 0
).
选择值将在窗口出现之前设置为设置(selection = 0
)。
If you want to run tests after mainloop()
, selection = var.get()
should also be after mainloop()
with tests.
如果你想在 之后运行测试mainloop()
,selection = var.get()
也应该在mainloop()
with 测试之后。
If you do not want to close the master window before tests, use command=function
:
如果您不想在测试前关闭主窗口,请使用command=function
:
from Tkinter import *
def function():
selection = var.get()
if selection == 1:
# Default
elif selection == 2:
# User-defined
else:#selection==0
#No choice
master.quit()
master = Tk()
var = IntVar()
Label(master, text = "Select OCR language").grid(row=0, sticky=W)
Radiobutton(master, text = "default", variable = var, value = 1).grid(row=1, sticky=W)
Radiobutton(master, text = "user-defined", variable = var, value = 2).grid(row=2, sticky=W)
Button(master, text = "OK", command = function).grid(row=3, sticky=W)
mainloop()