简体   繁体   English

Tkinter OptionMenu 不能在函数中使用 .get

[英]Tkinter OptionMenu cant use .get in a function

I'm writing some code and i need a variable to change when the optionMenu is changed here is some of my code below我正在编写一些代码,当 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()

This will not work in pycharm editor i get this error "unresolved attribute reference error on 'get' for Class 'OptionMenu'"这在 pycharm 编辑器中不起作用,我收到此错误“类 'OptionMenu' 的 'get' 上的未解决的属性引用错误”

and when i execute the code and try and change the OptionMenu i get this error in the console当我执行代码并尝试更改 OptionMenu 时,我在控制台中收到此错误

"StudentLoan = StudentLoanLi.get() AttributeError: OptionMenu instance has no attribute 'get'" “StudentLoan = StudentLoanLi.get() AttributeError: OptionMenu 实例没有属性‘get’”

any help will greatly appreciated任何帮助将不胜感激

The OptionMenu class has no get method. OptionMenu类没有get方法。 The correct way to get the selected item from an OptionMenu is to use the get method of the OptionMenu's StringVar , which you named StudentLoanOp :从 OptionMenu 获取所选项目的正确方法是使用 OptionMenu 的StringVarget方法,您将其命名为StudentLoanOp

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM