Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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
Python—使用Tkinter—在脚本之间保存单选按钮变量_Python_Button_Tkinter_Radio - Fatal编程技术网

Python—使用Tkinter—在脚本之间保存单选按钮变量

Python—使用Tkinter—在脚本之间保存单选按钮变量,python,button,tkinter,radio,Python,Button,Tkinter,Radio,我有一个使用tkinter的脚本。我已经在这个脚本中创建了预定义的单选按钮。我正在尝试从第二个脚本调用该脚本。当我运行原始脚本时,打印输出工作正常。但是,当我从第二个脚本运行它时,单选按钮var.get变量似乎没有在脚本之间进行转换,并且打印输出并不像预期的那样。我做错了什么?在此方面的任何帮助都将不胜感激 例如,当我从第一个脚本运行时,打印输出为: 你选择了北极 你选择了森林 你选择了草地 你选择了山 当我从第二张纸条运行时,打印输出为: 你已经选择了 你已经选择了 你已经选择了 你已经选择了

我有一个使用tkinter的脚本。我已经在这个脚本中创建了预定义的单选按钮。我正在尝试从第二个脚本调用该脚本。当我运行原始脚本时,打印输出工作正常。但是,当我从第二个脚本运行它时,单选按钮var.get变量似乎没有在脚本之间进行转换,并且打印输出并不像预期的那样。我做错了什么?在此方面的任何帮助都将不胜感激

例如,当我从第一个脚本运行时,打印输出为:

你选择了北极

你选择了森林

你选择了草地

你选择了山

当我从第二张纸条运行时,打印输出为:

你已经选择了

你已经选择了

你已经选择了

你已经选择了

-单选按钮选择丢失

其他详细信息:两个脚本保存在同一文件夹中。我尝试将选择设置为全局变量

要重现此错误,请执行以下操作:

1.将第一个脚本另存为两个单独的python脚本,第二个脚本保存在同一文件夹中

运行第一个脚本,进行无线电选择,打印消息将如预期的那样。你选择了北极

运行第二个脚本,在菜单中选择“创建”。选择获取怪物。选择单选按钮。打印输出将仅限于您选择的内容,不包括生物群落

第一个脚本:

from tkinter import *

def sel():
    selection = "You've selected " + var.get()
    poop = var.get()
    print(selection)
    return poop


root = Tk()
var = StringVar()

radio_frame = Frame(root, borderwidth=2,relief="groove")

## Radio buttons for choosing biome
biome_label=Label(radio_frame, text="Please choose a biome")
biome_label.pack()
search_biome1 = Radiobutton(radio_frame, text="Arctic", variable=var,
value="Arctic", command=sel, width=10, anchor=W)
search_biome1.pack()
search_biome2 = Radiobutton(radio_frame, text="Coast", variable=var,
value="Coast", command=sel, width=10, anchor=W)
search_biome2.pack()
search_biome3 = Radiobutton(radio_frame, text="Desert", variable=var,
value="Desert", command=sel, width=10, anchor=W)
search_biome3.pack()
search_biome4 = Radiobutton(radio_frame, text="Forest", variable=var,
value="Forest", command=sel, width=10, anchor=W)
search_biome4.pack()
search_biome5 = Radiobutton(radio_frame, text="Grassland", variable=var,
value="Grassland", command=sel, width=10, anchor=W)
search_biome5.pack()
search_biome6 = Radiobutton(radio_frame, text="Hill", variable=var,
value="Hill", command=sel, width=10, anchor=W)
search_biome6.pack()
search_biome7 = Radiobutton(radio_frame, text="Mountain", variable=var,
value="Mountain", command=sel, width=10, anchor=W)
search_biome7.pack()
search_biome8 = Radiobutton(radio_frame, text="Swamp", variable=var,
value="Swamp", command=sel, width=10, anchor=W)
search_biome8.pack()
search_biome9 = Radiobutton(radio_frame, text="Underdark", variable=var,
value="Underdark", command=sel, width=10, anchor=W)
search_biome9.pack()
search_biome10 = Radiobutton(radio_frame, text="Underwater", variable=var,
value="Underwater", command=sel, width=10, anchor=W)
search_biome10.pack()
search_biome11 = Radiobutton(radio_frame, text="Urban", variable=var,
value="Urban", command=sel, width=10, anchor=W)
search_biome11.pack()

radio_frame.grid()




root.mainloop()
第二个脚本:

from tkinter import *
from tkinter import messagebox



root = Tk()
root.title("Main")

def getMonsters():
    import DD_Enemy_Generator_Biome
    poop = DD_Enemy_Generator_Biome.sel()


menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Create", menu=filemenu)
filemenu.add_command(label="Get Monsters", command=getMonsters)
root.config(menu=menubar)
root.mainloop()

更改var.get对此的工作方式。。另外,在命令中使用lambda,它可以工作

def sel(biome_):
    global selection
    selection = "You've selected " + biome_
    poop = biome_
    print(selection)
    return poop

root = Tk()
var = StringVar()

radio_frame = Frame(root, borderwidth=2,relief="groove")

## Radio buttons for choosing biome
biome_label=Label(radio_frame, text="Please choose a biome")
biome_label.pack()

search_biome2 = Radiobutton(radio_frame, text="Coast", variable=var,
value="Coast", command=lambda value="Coast": sel(value), width=10, anchor=W)

现在真是魅力四射!谢谢你,巴基斯坦人!!