簡體   English   中英

學習python,卡在gui程序中無法運行

[英]Learning python, stuck in gui program that won't run

我一直在遵循一個教程,並編寫了該程序。 有人可以幫我弄清楚為什么我收到錯誤消息。 “應用程序實例沒有屬性'文本'”

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'

換線:

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

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

當您傳遞self.reveal()它會在定義self.texts之前調用self.reveal()

您要傳遞給函數而不是調用函數的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM