简体   繁体   English

Python Tkinter中的单选按钮值

[英]Radio button values in Python Tkinter

I am trying to create a dialog box in Python using Tkinter. 我正在尝试使用Tkinter在Python中创建一个对话框。 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." 单选按钮2选择选项“用户定义”。 The "OK" button closes the window. “确定”按钮关闭窗口。

Question 1: How do I save the value from the radio button? 问题1:如何通过单选按钮保存值? That is, how do I pass the selected radio button to the rest of my script? 也就是说,如何将选定的单选按钮传递给脚本的其余部分?

Question 2: How can I have the second radio button include user text input (along the lines of tkSimpleDialog.askstring)? 问题2:如何使第二个单选按钮包含用户文本输入(沿着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). 因此,整个对话框的顶部单选按钮应为普通单选按钮,第二个按钮指定用户输入并包括用于该用户输入的空间(以及OK按钮)。

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. 即使我选择单选按钮,选择也会返回0。
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...

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. 1)定义完后,我立即将var的值设置为1 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). 这是通过执行var.set(1)来完成的,并确保选择了第一个单选按钮(其值为1,如稍后在代码中定义的那样)。

2) I've replaced your master.quit command with a function called quit_loop . 2)我已经用一个名为quit_loop的函数替换了master.quit命令。 In this function: 在此功能中:

  • The var value is printed through a print and get statement. var值通过printget语句print The get will 'get' the current value of var , which depends on which radio button is selected. get将“获取” var的当前值,这取决于选择了哪个单选按钮。
  • I create a global variable within this function, which will then get the current value of var . 我在此函数内创建了一个全局变量,然后将get var的当前值。
  • I added parentheses to master.quit() because this is no longer in the command of a radio button. 我在master.quit()添加了括号,因为它不再位于单选按钮的命令中。 Note that if you plan on using IDLE, master.destroy() might be a more suitable alternative . 请注意,如果您打算使用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. 3)由于在函数中创建了selection变量,我们现在将所需的值存储在变量中。 There is one final if -statement at the end of the code to show it's working. 在代码末尾有一个最后的if -statement来表明它正在工作。

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. 1)先前包含"Select OCR language"标签将跨越三列, columnspan=3添加到网格参数中。 The same goes for your first radio button. 第一个单选按钮也是如此。

2) I added a Label and an Entry after your second radio button. 2)我在第二个单选按钮之后添加了LabelEntry Note that the columns go from 0 to 2, defining our three columns. 请注意,列从0到2,定义了我们的三列。 The label has a simple "Enter value:" text, whereas the entry has the variable textvariable=entry_text . 标签具有简单的"Enter value:"文本,而条目具有变量textvariable=entry_text I added this variable entry_text to the beginning of your code and immediately set its value to ### . 我将此变量entry_text添加到代码的开头,并立即将其值设置为### Note that this is a string (hence, text variable) so adding checks for integer numbers only is up to you. 请注意,这是一个string (因此为文本变量),因此仅对整数进行检查由您决定。

3) Of course, this is not linked to the second radio button. 3)当然,这没有链接到第二个单选按钮。 It still has a value of 2 if we select it, not the value of the Entry widget. 如果我们选择它,它的值仍然是2,而不是Entry小部件的值。 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. 这就是为什么在先前创建的quit_loop函数中,添加了一个小的if语句, if selection了第二个单选按钮,则该语句将条目的值分配给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. 如果这个简单的GUI仍然很小,则可以用这种方式编写代码。 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. 这样就不必事先定义它们。

Instead of directly using master.quit in the Button's command, define a function that finishes up the program then calls master.quit() : 而不是直接在Button的命令中使用master.quit ,而是定义一个函数来完成程序,然后调用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.quit()

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()之后。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM