Tkinter OptionMenu无法在函数中使用.get

时间:2018-04-13 12:16:37

标签: python python-2.7 tkinter optionmenu

我正在编写一些代码,我需要一个变量来更改optionMenu在这里更改是我的一些代码

#!/user
# -*- coding: utf-8 -*-

import locale
import Tkinter as Tk

root = Tk.Tk()
root.title("My Tax Calculator")
root.geometry("700x225")

def getStudentLoan():
    global StudentLoan
    StudentLoan = StudentLoanLi.get()

LeftFrame = Tk.Frame(root, width=300, height=200, pady=3)

Placeholder2 = Tk.Label(LeftFrame, text="")
Placeholder2.grid(row=2, column=1)

StudentLoanOp = Tk.StringVar()
StudentLoanOp.set("No")

StudentLoanLi = Tk.OptionMenu(Placeholder2, StudentLoanOp, "No", "Plan 1", "Plan 2", command=lambda _: getStudentLoan())
StudentLoanLi.grid(row=2, column=1)

Tk.mainloop()

这在pycharm编辑器中不起作用我得到这个错误“类'OptionMenu''获取'的未解决的属性引用错误”

当我执行代码并尝试更改OptionMenu时,我在控制台中收到此错误

“StudentLoan = StudentLoanLi.get() AttributeError:OptionMenu实例没有属性'get'“

任何帮助都将非常感谢

1 个答案:

答案 0 :(得分:3)

OptionMenu类没有get方法。从OptionMenu获取所选项目的正确方法是使用您命名为get的OptionMenu StringVar的{​​{1}}方法:

StudentLoanOp