【问题标题】:Learning python, stuck in gui program that won't run学习python,卡在无法运行的gui程序中
【发布时间】:2016-05-05 22:40:12
【问题描述】:

我一直在学习一个教程,并编写了这个程序。有人可以帮我弄清楚为什么我会收到错误消息。 "应用程序实例没有属性'text'"

from Tkinter import *

class Application(Frame):
    """ A GUI application with three buttons."""

    def __init__(self,master):
        """ This initialized the Frame"""
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
        self.buttonclicks = 0

    def create_widgets(self):
        """Create a button that measures clicks."""
        #create first button
        self.button1 = Button(self, text = "Total Clicks: 0")
        self.button1.grid()

        self.instruction = Label(self, text= "Enters the password")
        self.instruction.grid(row=0, column=0, columnspan=2, sticky=W)

        self.password = Entry(self)
        self.password.grid(row=1,column=1, sticky=W)

        self.button1["command"] = self.update_count
        self.button1.grid(row=2,column=0,sticky=W)

        self.submit_button = Button(self, text="Submit", command=self.reveal())

        self.texts = Text(self, width=35, height=5,wrap=WORD)
        self.texts.grid(row=4,column=1,columnspan=2,sticky=W)

    def update_count(self):
        """Increase this click count and display the new total"""
        self.buttonclicks += 1
        self.button1["text"] = "Total Clicks: " + str(self.buttonclicks)

    def reveal(self):
        """Reveal the password"""
        content = self.password.get()

        messsage=""

        if content == "password":
            message = "You have access to the data."
        else:
            message = "Access denied."

        self.texts.insert(0.0,message)



root = Tk()

root.title("GUI test")
root.geometry("250x185")

app = Application(root)

root.mainloop()

问题是我已经有一个定义为“文本”的文本字段,如果它可以从属性中获取密码,我认为它没有理由无法获取文本。

编辑:被问及错误:

   Traceback (most recent call last):
  File "clickcounter.py", line 58, in <module>
    app = Application(root)
  File "clickcounter.py", line 10, in __init__
    self.create_widgets()
  File "clickcounter.py", line 28, in create_widgets
    self.submit_button = Button(self, text="Submit", command=self.reveal())
  File "clickcounter.py", line 49, in reveal
    self.texts.insert(0.0,message)
  AttributeError: Application instance has no attribute 'texts'

【问题讨论】:

  • 您能否包含提供该错误消息的堆栈跟踪?它使您可以更轻松地查看您的代码。
  • 作为一般规则,为了成为一个挑剔的人,尽量避免在 python 中导入* glob。如果您显式导入每件事(即从Tkinter import Text, Entry, Button, Label)或只执行import Tkinter 并显式调用所有内容,则调试代码会更加清晰和容易。 Tkinter.Text(...)
  • 好提示。我在原始帖子中添加了错误消息。

标签: python user-interface button tkinter


【解决方案1】:

换行:

self.submit_button = Button(self, text="Submit", command=self.reveal())

self.submit_button = Button(self, text="Submit", command=self.reveal)

当您传递 self.reveal() 时,它会在 self.reveal() 定义之前调用 self.reveal()

您想将要调用的函数传递给它,而不是调用函数的结果。

【讨论】:

  • 是的,已经解决了,非常感谢,我没有意识到你可以通过不使用括号来调用函数而不调用它。很高兴我知道了!
  • 是的,事实上使用 Python 几乎任何东西都可以这样设置。例如,您可以说foo = self.reveal,然后再致电foo(...)
  • @twoshoes 在 Python 中,基本上everythingfirst class object。而在 Python 中,我们所说的变量实际上更像是挂在对象上的“标签”。代码x=4 将标签x 挂在整数对象4 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多