1

我正在编写一些代码,当 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' 的 'get' 上未解决的属性引用错误”

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

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

任何帮助将不胜感激

4

1 回答 1

7

该类OptionMenu没有get方法。从 OptionMenu 获取所选项目的正确方法是使用getOptionMenu's的方法StringVar,您将其命名为StudentLoanOp

def getStudentLoan():
    global StudentLoan
    StudentLoan = StudentLoanOp.get()
于 2018-04-13T12:24:46.987 回答