【问题标题】:Python won't recognise attributePython 无法识别属性
【发布时间】:2012-08-11 14:43:04
【问题描述】:

我正在编写我的第一个 GUI 应用程序来为一家公司查找产品。

from Tkinter import *
import tkMessageBox

def debug():
    buttonText = relStatus.get()
    tkMessageBox.showinfo("You Clicked ", buttonText)
    return

app = Tk()
app.title("Ironcraft Product Finder")
app.geometry("700x500")

labelText = StringVar()
labelText.set("Choose an Appliance Type")
topLabel = Label(app, textvariable = labelText, height = 5).pack()

fire = Button(app, text="Fire", width=20, command=debug)
fire.pack(padx=10)

relStatus = StringVar()
relStatus.set(fire.text)

app.mainloop()

当我运行它时,它会出现错误消息:

AttributeError: Button instance has no attribute 'text'

但在“火”的创造中它说

text="fire"

这不是属性吗?

【问题讨论】:

    标签: python oop user-interface tkinter


    【解决方案1】:
    topLabel = Label(app, textvariable = labelText, height = 5).pack()  # so topLabel is None
    
    topLabel = Label(app, textvariable = labelText, height = 5)
    topLabel.pack() # topLabel is Label
    

    【讨论】:

      【解决方案2】:

      Tkinter 模块有点老派; text 值可以通过项目查找来访问:

      relStatus.set(fire['text'])
      

      请参阅 Tkinter 文档的 Setting Options section

      【讨论】:

      • 谢谢,我自己永远不会得到那个:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多