2009-07-03 73 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 
>>> 

這是當我評論一號線

可能becoz MYAPP沒有找到父母。

但這個概念並不清楚。

任何人能詳細解釋這一概念....

回答

0

指定傳入的參數父母self.parent?

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

你究竟爲什麼你註釋掉這兩條線提self.myparent並創建一個新提一個神祕的,從來沒有初始化self.parent?!當然,這就是你的問題的開始 - 看起來像荒謬的,蓄意破壞代碼。

0

你的問題不是tkinter相關的,而是關於面向對象的設計。

對myApp具有__init__方法(構造器,創建該類的對象時執行所述方法),以及兩種方法按鈕點擊。在button2Click方法中,您嘗試讀取屬性self.parent(翻譯爲myapp.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)