2015-05-04 48 views
-2

我試圖訪問另一個類中的函數,並通過「主」類來執行該操作。我已經通過Column 4按鈕成功地訪問了「master」類,我試圖通過列5函數訪問主類,然後使其進入窗口類的反應函數,但是當我嘗試這樣做時,它失敗。Python:在另一個類中訪問函數

from tkinter import * 

class Window(): 
    def __init__(self, parent, parent_class2): 
     self.parent = parent 
     self.parent_class = parent_class2 
     self.canvas = Canvas(self.parent, width=420, height=360) 
     self.canvas.pack(side="top", fill="both", expand="true") 
     self.cellwidth = 60 
     self.cellheight = 60 
     self.rows = 6 
     self.columns = 7 
     self.rect = {} 
     self.oval = {} 
     self.piece = [] 
     #creates the grid 
     for row in range(6): 
      for column in range(7): 
       x1 = column*self.cellwidth 
       y1 = row * self.cellheight 
       x2 = x1 + self.cellwidth 
       y2 = y1 + self.cellheight 
       self.piece.append(Piece(self.canvas, x1,y1,x2,y2)) 

     self.canvas.itemconfig(self.piece[8].oval, fill="deep pink") 

    def reaction(self): 
     print("In WIndow Class - SUCCESS!") 

class ButtonsExampleGUI: 
    def __init__(self, parent, parent_class): 

     self.parent_class = parent_class 
     self.parent = parent 
     #self.buttons = 7 

     c4 = Button(parent,text = ("Column 4"), command = self.c4_played) 
     c4.pack(side = LEFT) 
     c5 = Button(parent,text = ("Column 5"), command = self.c5_played) 
     c5.pack(side = LEFT) 


    def c4_played(self): 
     self.parent_class.test() 
     print("Col 4") 

    def c5_played(self): 
     print("Col 5") 
     self.parent_class.towindow() 

class Piece: 
    def __init__(self, parent_canvas, x1,y1,x2,y2): 
     self.parent_canvas = parent_canvas 
     self.rect = self.parent_canvas.create_rectangle(x1,y1,x2,y2, fill="grey", tags="rect") 
     self.oval = self.parent_canvas.create_oval(x1+2,y1+2,x2-2,y2-2, fill="white", tags="oval") 

#self class here is being taken as the "parent_class" of the ButtonExampleGUI class 
class Game: 
    def __init__(self, parent): 
     self.parent = parent 
     self.window = Window (self.parent, self) 
     self.buttons = ButtonsExampleGUI (self.parent, self) 

#test being accessed by c4 function calling this within a different class 
    def test(self): 
     print("from parent class") 

    def towindow(self): 
     print("In Game Class") 
     self.parent_class2.reaction() 

if __name__ == "__main__": 
    root = Tk() 
    game = Game(root) 
    root.mainloop() 
+0

你能提供有關此失敗的具體方式更詳細?如果有例外,你可以提供這個例外嗎? –

+0

@ eric-scrivner下面是錯誤'Tkinter回調中的異常 Traceback(最近調用最後一次): __call__中的第1533行文件「C:\ Python34 \ lib \ tkinter \ __ init__.py」返回self.func * args) 文件「H:\ 2015 DTS \ 3.46 \ C4 Revisions \ 4May.py」,第77行,在c5_played中 self.parent_class.towindow() 文件「H:\ 2015 DTS \ 3.46 \ C4 Revisions \ 4May .py「,第109行,在towindow中 self.parent_class2.reaction() AttributeError:'遊戲'對象沒有任何屬性'parent_class2'' – PaddyGower

+0

應該使用類遊戲擴展tkinter嗎? – Scott

回答

1

也許是因爲你沒有在遊戲中定義parent_class2?請嘗試更改高清towindow(個體經營):

來自:

def towindow(self): 
    print("In Game Class") 
    self.parent_class2.reaction() 

到:

def towindow(self):    
    print("In Game Class") 
    self.window.reaction() 
相關問題