首页 > 解决方案 > Python - 使用 Tkinter——在脚本之间保存单选按钮变量

问题描述

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

前任。当我从第一个脚本运行时,打印输出是:

您选择了北极

你选择了森林

你选择了草原

你选择了山

当我从第二个脚本运行时,打印输出是:

您已选择

您已选择

您已选择

您已选择

--缺少单选按钮选择

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

要重现此错误:

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

  1. 运行第一个脚本,选择单选,打印消息将如预期的那样。例如“你选择了北极”

  2. 运行第二个脚本,在菜单中选择“创建”。选择“获得怪物”。进行单选按钮选择。打印出来的只是“您已选择”它不包括生物群系

第一个脚本:

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

标签: pythonbuttontkinterradio

解决方案


改变 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)

推荐阅读