2013-05-06 7 views
0

과제를위한 Tkinter GUI를 작성 중이며 클래스 메소드에 액세스 할 때 몇 가지 문제가 발생했습니다. 여기에 붙여 넣은 코드의 첫 번째 비트는 최상위 인터페이스 클래스입니다. 코드의 주요 기능은 "master"가 Tk() 인스턴스이고이 클래스의 인스턴스를 만듭니다.Python 2.7의 다른 클래스에서 클래스 메서드에 액세스

class PlotApp(object): 
    """ 
    Top level interface. Contains canvas and other widgets. 
    """ 

    def __init__(self, master): 

     # Initialise variables, world screen class and window features 
     master.wm_minsize(740, 480) 
     master.configure(bg = "gray80") 
     self.isFunctionDrawn = False   

     # Create objects 
     self.pointf = PointFrame(master) 
     self.canvas = Canvas(master, bg = "white", bd = 2, relief = SUNKEN, 
          highlightbackground = "gray80") 
     self.canvas.pack(expand = True, fill = BOTH, padx = 10, side = TOP) 
     self.functionf = FunctionFrame(master) 
     self.plotf = PlotFrame(master) 
     self.buttonf = ButtonFrame(master, self.canvas) 

self.functionfgetFunction()라는 메소드를 포함 내 FunctionFrame 클래스의 인스턴스입니다. 이 메서드에 액세스하려면 ButtonFrame 클래스 인스턴스의 단추를 원하지만이 작업을 수행하는 방법을 모르겠습니다. 나는 시도했다 :

def testFunction(self): 
    self.parent.functionf.getFunction() 

(parent 코드의 첫 번째 비트에서 master 인수 곳이다) 그러나 이것은 분명히 작동하지 않을 Tk()의 객체로 functionf를 호출하는 것으로 보인다. 이 주위에 어떤 방법이 있습니까?

답변

0

master가 Tk() 인스턴스입니다. ButtonFrame 클래스에서 변수 parent을 호출했다고해서 그것이 인스턴스를 만든 객체라는 것을 의미하지는 않습니다.

당신은 (당신이 당신의 ButtonFrame에 master 객체를 필요로하는 경우와, PlotApp에서 self.master 같은에서 마스터를 저장) 부모 PlotApp에서 self에 전달하거나 필요, 또는 당신은 또한 당신의 PlotApp 인스턴스의 self 변수에 전달해야 귀하의 ButtonFrame.

+0

감사합니다 힙 언더런, 트릭 완료되었습니다. – Drizzlydayz

관련 문제