Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter packge for python,如何返回所选选项?_Python_Python 3.x_Tkinter - Fatal编程技术网

Tkinter packge for python,如何返回所选选项?

Tkinter packge for python,如何返回所选选项?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在学习tkinter,并希望在我的项目中使用它,但我一直在思考如何做到这一点。我想知道代码如何返回所选选项 代码如下: 从tkinter导入* 从tkinter.ttk导入* master=Tk() 几何硕士(“175x200”) v=StringVar(主“1”) 选项={ “单选按钮1”:“1”, “单选按钮2”:“2”, “单选按钮3”:“3”, “单选按钮4”:“4”, “单选按钮5”:“5” } 对于options.items()中的(文本、值): 单选按钮(主控,文本=文本,变量

我正在学习
tkinter
,并希望在我的项目中使用它,但我一直在思考如何做到这一点。我想知道代码如何返回所选选项

代码如下:

从tkinter导入*
从tkinter.ttk导入*
master=Tk()
几何硕士(“175x200”)
v=StringVar(主“1”)
选项={
“单选按钮1”:“1”,
“单选按钮2”:“2”,
“单选按钮3”:“3”,
“单选按钮4”:“4”,
“单选按钮5”:“5”
}
对于options.items()中的(文本、值):
单选按钮(主控,文本=文本,变量=v,
值=值)。包装(侧面=顶部,ipady=5)
打印(StringVar())
退出按钮(master,text=“quit”,command=master.quit,宽度=10)
退出_btn.pack()
mainloop()
def selected_option():
返回选项。获取(文本、值)
打印(所选选项())

虽然还有一些小错误,但要点是只在按下按钮后调用
v.get()
。因此,您必须将其放入函数中,就像我使用
quitbutton()
函数所做的那样。该函数将在单击按钮时立即调用,因为它显示
command=quitbutton

此外,您还尝试打印
StringVar()
,但不应该这样做。您创建了一个变量
v
,它是StringVar。之后,您应该调用该v而不是StringVar。因此,与其执行
print(StringVar)
,不如执行
print(v.get())

我还建议您在Youtube上观看一些关于Tkinter的教程,因为这将增强您对基本原理的理解。您的代码非常好,但它缺少一些基础知识,这使得它无法按您希望的方式工作

from tkinter import *
from tkinter.ttk import *

master = Tk()
master.geometry("175x200")

# Changed this do StringVar() without anything in it and set the beginning value to 1
v = StringVar(master)
v.set(1)

options = {
          "RadioButton 1": "1",
          "RadioButton 2": "2",
          "RadioButton 3": "3",
          "RadioButton 4": "4",
          "RadioButton 5": "5"
}

for (text, value) in options.items():
    Radiobutton(master, text=text, variable=v,
                value=value).pack(side=TOP, ipady=5)

# Created a function that runs every time the button gets clicked (see the command=quitbutton in the Button widget) and gets the value of the button that is selected
def quitbutton():
    print(v.get())
    # master.quit() uncomment this line if you want to close the window after clicking the button


# Changed the function which gets called by changing the word after command=
quit_btn = Button(master, text="Quit", command=quitbutton, width=10)
quit_btn.pack()

mainloop()

虽然还有一些小错误,但要点是只在按下按钮后调用
v.get()
。因此,您必须将其放入函数中,就像我使用
quitbutton()
函数所做的那样。该函数将在单击按钮时立即调用,因为它显示
command=quitbutton

此外,您还尝试打印
StringVar()
,但不应该这样做。您创建了一个变量
v
,它是StringVar。之后,您应该调用该v而不是StringVar。因此,与其执行
print(StringVar)
,不如执行
print(v.get())

我还建议您在Youtube上观看一些关于Tkinter的教程,因为这将增强您对基本原理的理解。您的代码非常好,但它缺少一些基础知识,这使得它无法按您希望的方式工作

from tkinter import *
from tkinter.ttk import *

master = Tk()
master.geometry("175x200")

# Changed this do StringVar() without anything in it and set the beginning value to 1
v = StringVar(master)
v.set(1)

options = {
          "RadioButton 1": "1",
          "RadioButton 2": "2",
          "RadioButton 3": "3",
          "RadioButton 4": "4",
          "RadioButton 5": "5"
}

for (text, value) in options.items():
    Radiobutton(master, text=text, variable=v,
                value=value).pack(side=TOP, ipady=5)

# Created a function that runs every time the button gets clicked (see the command=quitbutton in the Button widget) and gets the value of the button that is selected
def quitbutton():
    print(v.get())
    # master.quit() uncomment this line if you want to close the window after clicking the button


# Changed the function which gets called by changing the word after command=
quit_btn = Button(master, text="Quit", command=quitbutton, width=10)
quit_btn.pack()

mainloop()

很高兴我能帮忙。快乐编码!很高兴我能帮忙。快乐编码!