【问题标题】:Getting Pick list selected value from other Python file从其他 Python 文件中获取 Pick list 选定值
【发布时间】:2021-03-28 21:08:14
【问题描述】:

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

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

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

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

from input import Input

print(Input.Budget)
print (var1.get())

它确实给了我所有的“预算”列表,但选择了机器人的值。这是错误: NameError: name 'var1' is not defined 我应该在哪里申报?

谢谢!

【问题讨论】:

  • 这个问题有点模糊,难以重现。您是否尝试从另一个文件中获取变量 var1?
  • @ObjectJosh 是的,因为我相信它会给我从选择列表中选择的值。

标签: python class oop variables picklist


【解决方案1】:

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

a = Input()
print(a.var1)

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

【讨论】:

  • 我初始化了 var1,我确实在另一个 python 文件中得到了他,但我没有从选择列表中获得选定的值。也许 var1 不是用户选择?谢谢
  • 您的 GUI 使用的是什么? PyQt5?
  • 我正在使用 TKInter。
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
相关资源
最近更新 更多