Python Tkinter 中的单选按钮值 [英] Radio button values in Python Tkinter

查看:171
本文介绍了Python Tkinter 中的单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 在 Python 中创建一个对话框.目标是有一个带有两个单选按钮和一个确定"按钮的对话框.单选按钮一选择选项默认".单选按钮二选择用户定义"选项.确定"按钮关闭窗口.

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.

问题 1: 如何保存单选按钮的值?也就是说,如何将选定的单选按钮传递给脚本的其余部分?

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?

问题 2: 如何让第二个单选按钮包含用户文本输入(沿着 tkSimpleDialog.askstring 行)?我希望该按钮显示一个单选按钮、一个提示(输入值:")和一个供用户输入文本的空间——所有这些都在一行中作为一个单选按钮选项.
所以整个对话框的顶部单选按钮应该是一个普通的单选按钮,第二个按钮指定用户输入并为该用户输入包含一个空格(和 OK 按钮).

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).

到目前为止,我打开了一个带有两个选项的对话框,但该值没有传递给我能看到的任何内容;即使在我选择单选按钮之前,selection 也会返回为 0.
对任何一个问题的任何帮助将不胜感激,谢谢.
到目前为止,这是我的脚本:

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:

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...

推荐答案

参加聚会有点晚,但我在尝试在 Tkinter 单选按钮上查找内容时偶然发现了这个问题.

A bit late to the party, but I stumbled upon this question while trying to find something on Tkinter radiobuttons.

我改变了三件事:

1) 在您定义后,我立即将 var 的值设置为 1.这是通过执行 var.set(1) 完成的,并确保您的第一个单选按钮被选中(其值为 1,正如您稍后在代码中定义的那样).

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).

2) 我已将您的 master.quit 命令替换为一个名为 quit_loop 的函数.在这个函数中:

2) I've replaced your master.quit command with a function called quit_loop. In this function:

  • var 值通过 printget 语句打印.get 将获取"var 的当前值,这取决于选择的单选按钮.
  • 我在这个函数中创建了一个全局变量,然后它会getvar的当前值.
  • 我为 master.quit() 添加了括号,因为这不再是单选按钮的命令.请注意,如果您打算使用 IDLE,master.destroy() 可能是 更合适的替代方案.
  • The var value is printed through a print and get statement. The get will 'get' the current value of var, which depends on which radio button is selected.
  • I create a global variable within this function, which will then get the current value of var.
  • 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.

3) 由于在函数中创建了 selection 变量,我们现在将您想要的值存储在一个变量中.代码末尾有一个最后的 if 语句来表明它正在工作.

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.

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."

<小时>

问题 2:

我会保持简单,只需在您的单选按钮后面添加一个标签和一个输入框.这意味着我们还必须处理列,因为您在之前的代码中没有定义任何列,这使得所有内容都默认为列 0.我们希望您的第二个单选按钮是radio、label、entry",它需要三列.


Question 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.

1) 包含 选择 OCR 语言" 的前一个标签将跨越三列,并将 columnspan=3 添加到网格参数中.您的第一个单选按钮也是如此.

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.

2) 我在您的第二个单选按钮之后添加了一个 Label 和一个 Entry.请注意,列从 0 到 2,定义了我们的三列.标签具有简单的 "Enter value:" 文本,而条目具有变量 textvariable=entry_text.我将此变量 entry_text 添加到代码的开头,并立即将其值设置为 ###.请注意,这是一个 string(因此是 text 变量),因此仅添加对整数的检查由您决定.

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.

3) 当然,这与第二个单选按钮无关.如果我们选择它,它的值仍然是 2,而不是 Entry 小部件的值.这就是为什么在之前创建的 quit_loop 函数中,我添加了一个小的 if 语句,该语句将条目的值分配给 selection 如果第二个收音机按钮被选中.

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.

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

<小时>

提示

如果这个简单的 GUI 保持这么小,那么以这种方式编写代码是可以的.但是,在这方面进一步扩展,我建议采用面向对象的方法 因为它确实大大提高了可读性,尤其是在定义函数时.这样就不必事先定义它们.


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 approach as it really improves readability a lot, especially when functions are being defined. That way they don't have to be necessarily defined beforehand.

这篇关于Python Tkinter 中的单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆