2016-05-05 9 views
0

私はチュートリアルに従っていて、このプログラムを書いています。誰かが私がエラーメッセージを受け取っている理由を理解する手助けをすることができますか? 「アプリケーションのインスタンスには属性 『テキスト』を持っていない」Pythonを学ぶ、実行されないguiプログラムでスタック

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' 
+1

エラーメッセージを表示するスタックトレースを含めることはできますか?それはあなたのコードを見て簡単になります。 –

+1

一般的なルールとしても、ちょうどnit-pickにすることは、Pythonでglob importを避けるようにしてください。明示的に各項目をインポートする(つまり、 'Tkinter import Text、Entry、Button、Label'から)、あるいは' import Tkinter'を実行してすべてを明示的に呼び出すと、コードをデバッグするのがはるかに簡単で簡単です。 'Tkinter.Text(...)' –

+0

良いヒント。元の投稿にエラーメッセージを追加しました。 – twoshoes

答えて

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.textsが定義される前に。

呼び出す関数の結果ではなく、呼び出す関数に渡したいとします。

+0

はい、それを修正しました、ありがとう、括弧を使用しないで関数を渡すことはできませんでした。うれしい私はそれを学んだ! – twoshoes

+1

はい、実際にはPythonでほとんどのものはそのように設定できます。たとえば、あなたは 'foo = self.reveal'と言うことができ、その後' foo(...) 'を呼び出すことができます –

+1

@twoshoes Pythonでは、基本的に*すべて*は[first classオブジェクト]です(http:// python-history .blogspot.nl/2009/02/first-class-everything.html)。 Pythonでは、変数と呼ばれるものは、実際にはオブジェクトにぶら下がっている「ラベル」によく似ています。コード 'x = 4'は整数オブジェクト' 4'にラベル 'x'を掛けます。 –

関連する問題