Python:Aaccess是另一个类中的一个函数

时间:2015-05-04 02:44:49

标签: python python-3.x

我试图访问另一个类中的一个函数,然后通过一个" master"类。我已成功访问" master"通过第4列按钮,我尝试通过第5列函数访问主类,然后将其转到窗口类中的反应函数,但是当我尝试这个时,它会失败。

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()

1 个答案:

答案 0 :(得分:1)

也许是因为你没有在Game中定义parent_class2?尝试更改def towindow(自我):

从:

CREATE PROCEDURE NextID 
    @pLastUsed CHAR(3) 
AS
/* Usage
EXEC NextID 'J64'

*/
BEGIN
    SET NOCOUNT ON;
  DECLARE @T1 TABLE (Col CHAR(1))
  DECLARE @T2 TABLE (Col CHAR(1))
  DECLARE @T3 TABLE (Col CHAR(1))
  INSERT @T1 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9' UNION ALL SELECT 'A' UNION ALL SELECT 'B' UNION ALL SELECT 'C'
                                UNION ALL SELECT 'D' UNION ALL SELECT 'E' UNION ALL SELECT 'F' UNION ALL SELECT 'G'
                                UNION ALL SELECT 'H' UNION ALL SELECT 'I' UNION ALL SELECT 'J' UNION ALL SELECT 'K'
                                UNION ALL SELECT 'L' UNION ALL SELECT 'M' UNION ALL SELECT 'N' UNION ALL SELECT 'O'
                                UNION ALL SELECT 'P' UNION ALL SELECT 'Q' UNION ALL SELECT 'R' UNION ALL SELECT 'S'
                                UNION ALL SELECT 'T' UNION ALL SELECT 'U' UNION ALL SELECT 'V' UNION ALL SELECT 'W'
                                UNION ALL SELECT 'X' UNION ALL SELECT 'Y' UNION ALL SELECT 'Z'
  INSERT @T2 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9'
  INSERT @T3 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9'
  SELECT TOP 1
    t1.Col + t2.Col + t3.Col
  FROM
    @T1 t1
    CROSS JOIN @T2 AS t2
    CROSS JOIN @T3 AS t3
  WHERE
    t1.Col + t2.Col + t3.Col > @pLastUsed
  ORDER BY
    t1.Col + t2.Col + t3.Col
END
GO

为:

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