从其他 Python 文件中获取选择列表选定值

问题描述

我正在尝试从同一项目中的另一个 Pyton 文件获取下拉列表的值。

enter image description here

这是我在 input.py 文件中的代码

type
  EM_COLOR_TYPE = (
    NET_COLOR_TYPE_RED,// red
    NET_COLOR_TYPE_YELLOW,// yellow
    NET_COLOR_TYPE_GREEN,// green
    NET_COLOR_TYPE_CYAN,// cyan
    NET_COLOR_TYPE_BLUE,// glue
    NET_COLOR_TYPE_PURPLE,// purple
    NET_COLOR_TYPE_BLACK,// black
    NET_COLOR_TYPE_WHITE,// white
    NET_COLOR_TYPE_MAX
 );

byColorSimilar: array[0..NET_COLOR_TYPE_MAX-1] of Byte;
byUpperBodyColorSimilar: array[0..NET_COLOR_TYPE_MAX-1] of Byte;
byLowerBodyColorSimilar: array[0..NET_COLOR_TYPE_MAX-1] of Byte;

这是我得到的另一个文件中的代码,我想获取 pl1 的选定值(获取预算列表选定值)。

class Input:

Budget = {'Flexible','Variable','Fixed'}

def __init__(self):

    root =Tk()
    root.title('Input window V1')
    root.geometry('1300x690')
    root.resizable(False,False)

    frame = Frame(root,width=1000,height=680)
    frame.configure(background="gray28")
    frame.pack(fill=BOTH,expand=True)

    lbl1 = Label(root,bg="gray28",pady=1,text='Budget:',fg="cyan2",font=("Helvetica",20))
    lbl1.place(x=240,y=225)

    var1 = StringVar(root)
    #var1.set("None") # default value (Not in use)
    pl1 = OptionMenu(root,var1,*self.Budget )
    pl1.config(width=20,bg="GREEN",fg="white")
    pl1.place(x=470,y=230)

    button1 = Button(root,text="GO Hybrid !",command=dashboard)
    button1.config(width=25,bg="white")
    button1.place(x=600,y=590)

    root.mainloop()

它确实为我提供了所有“预算”列表,但会显示所选值。这是错误NameError:未定义名称“var1” 我应该在哪里申报?

谢谢!

解决方法

如果您尝试从另一个文件中获取 var1,请先初始化您的对象。

a = Input()
print(a.var1)

在您的 __init__ 中: self.var1 = StringVar(root)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...