2009-07-03 6 views
0

は、これは私のコードです:Tkinterのボタンバインドと親deatroy

print '1' 
from Tkinter import * 
print '2' 
class myApp: 

    print '3' 
    def __init__(self,parent): 
     print '4' 

##  self.myparent = parent line1 
     print '11' 

     self.myContainer1 = Frame(parent) 
     print '12' 
     self.myContainer1.pack() 
     print '13' 

     self.button1 = Button(self.myContainer1,text="OK",background="green") 
     print '14' 
     self.button1.pack(side=LEFT) 
     print '15' 
     self.button1.bind("<Button-1>",self.button1Click) 
     print '16' 

     self.button2 = Button(self.myContainer1,text="Cancel",background="cyan") 
     print '17' 
     self.button2.pack(side=RIGHT) 
     print '18' 
     self.button2.bind("<Button-1>",self.button2Click) 
     print '19' 


    def button1Click(self,event): 

      print '5' 

      if self.button1['background'] == 'green': 
       print '20' 
       self.button1['background'] ='tan' 
       print '21' 

      else: 

       print '22' 

       self.button1['background'] = 'yellow' 

       print '23' 

    def button2Click(self,event): 

      print '6' 

##   self.myparent.destroy() 

      self.parent.destroy() 

print '7' 
root = Tk() 
print '8' 
myapp = myApp(root) 
print '9' 
root.mainloop() 
print '10' 

エラーは次のとおりです。

>>> ================================ RESTART ================================ 
>>> 
1 
2 
3 
7 
8 
4 
11 
12 
13 
14 
15 
16 
17 
18 
19 
9 
5 
20 
21 
5 
22 
23 
6 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python26\lib\lib-tk\Tkinter.py", line 1403, in __call__ 
    return self.func(*args) 
    File "C:\Documents and Settings\he00044.HALEDGEWOOD\Desktop\TkinterExamples\buttonBind.py", line 56, in button2Click 
    self.parent.destroy() 
AttributeError: myApp instance has no attribute 'parent' 
10 
>>> 

これは私がLINE1

コメントするときには、myappにbecozかもしれ親を見つけていません。

しかし、そのコンセプトは明確ではありません。

誰もが

答えて

0

がself.parent着信パラメータの親を割り当てます....詳細に概念を説明することはできますか?

def __init__(self,parent): 
    self.parent = parent 
2

なぜ今まであなたはself.myparentに言及し、これらの2行をコメントアウトし、神秘的に言及し、新しいものを作成し、self.parent-初期化されませんでした?!これはあなたの問題の始まりです。もちろん、不条理で意図的なコードの妨害に見えます。

0

あなたの質問はtkinter関連ではなく、むしろオブジェクト指向設計です。

クラスて、myApp__init__方法(コンストラクタ、そのクラスのオブジェクトが作成されるときに実行される方法)、ならびにボタンをクリックする2つの方法を持っています。 button2Clickメソッドでは、属性self.parentmyapp.parentと変換)を読み取ろうとしますが、このプロパティは定義されていません。

1行目のコメントを外すと、エラーは発生しません。これは、属性myapp.parentを作成し、この属性にTkルートウィジェットを割り当てているためです。これは、作成するすべてのウィジェットが親ウィジェットを受け取る必要があるため、必要です。

0

これまでのその他の回答は素晴らしいです。
これも役立ちます:Fredrik Lundh's intro to Tkinter

追加他の回答と一緒に、あなたは再び移動を取得助けるべきである、あなたのコードにいくつかのコメント:

import Tkinter 

class MyApp: 
    def __init__(self, parent): # constructor 
     self.parent = parent # the parent here is 'root' 
     self.myContainer1 = Tkinter.Frame(self.parent) # create Frame w/ root as parent 
     self.myContainer1.pack() # make Frame (myContainer1) visible 
     self.button1 = Tkinter.Button(self.myContainer1, 
         text="OK", background="green") # add button as child of Frame 
     self.button1.pack(side=Tkinter.LEFT) # place button1 in Frame 
     self.button1.bind("<Button-1>",self.button1Click) # bind left mouse button to button1Click method 
     self.button2 = Tkinter.Button(self.myContainer1, text="Cancel", 
         background="cyan") 
     self.button2.pack(side=Tkinter.RIGHT) 
     self.button2.bind("<Button-1>", self.button2Click) 

    def button1Click(self, event): 
     if self.button1['background'] == 'green': 
      self.button1['background'] ='tan' 
     else: 
      self.button1['background'] = 'yellow' 

    def button2Click(self, event): 
     self.parent.destroy() # the parent here is 'root', so you're ending the event loop 

root = Tkinter.Tk()  # create root widget (a window) 
myapp = MyApp(root)  # create instance of MyApp with root as the parent 
root.mainloop()   # create event loop (ends when window is closed)