2013-05-06 118 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.functionf是我FunctionFrame類,它包含一個名爲getFunction()方法的一個實例。我想要一個​​類實例中的按鈕來訪問此方法,但我不知道如何執行此操作。我已經試過:

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

(其中parent是在代碼的第一位master參數),但這似乎調用functionfTk()對象,這顯然是行不通的。有沒有辦法解決?

回答

0

master是你的Tk()實例。僅僅因爲你在你的ButtonFrame類中調用了變量parent並不意味着它是創建實例的對象。

你需要無論是在selfPlotApp通過爲父母(如果你需要你的master對象在ButtonFrame,保存大師像在PlotAppself.master),或者你也需要通過在PlotApp實例的self變量你的​​。

+0

感謝堆積不足,這是做到了。 – Drizzlydayz 2013-05-06 12:47:56

相關問題