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

Python - Using Tkinter--saving radio button variables between scripts

我有一个使用 tkinter 的脚本。我在此脚本中创建了预定义的单选按钮。我正在尝试从第二个脚本调用脚本。当我 运行 原始脚本时,打印出来的效果很好。但是,当我从第二个脚本 运行 时,单选按钮 var.get() 变量似乎没有在脚本之间进行转换,打印输出也不像预期的那样。我究竟做错了什么?如有任何帮助,我们将不胜感激!!

例如。当我从第一个脚本 运行 打印出来时:

您 select 探索了北极

您 select 森林

您select编辑了草原

您 select 登上了 Mountain

当我从第二张纸条 运行 打印出来时:

您 select 编辑了

您 select 编辑了

您 select 编辑了

您 select 编辑了

--缺少单选按钮selection

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

要重现此错误:

1.save 作为同一文件夹中的两个单独的 python 脚本(第一个脚本,第二个脚本)。

  1. 运行 第一个脚本,做一个radio selection,打印消息就如预期的那样。例如 "You've selected Arctic"

  2. 运行第二个脚本,select"create"在菜单中。 Select "Get Monsters"。制作一个单选按钮 selection。打印输出只会是 "You've selected" 它不包括生物群落

第一个脚本:

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)