0

割り当て用の Tkinter GUI を作成していますが、クラス メソッドへのアクセスで問題が発生しています。ここに貼り付けた最初のコードは、最上位のインターフェイス クラスです。コードのメイン関数は、「マスター」が Tk() のインスタンスであるこのクラスのインスタンスを作成します。

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.functionfFunctionFrameと呼ばれるメソッドを含む私のクラスのインスタンスですgetFunction()。クラス インスタンスのボタンでこのメソッドにアクセスしたいのですが、そのButtonFrame方法がわかりません。私はもう試した:

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

( whereparentmasterコードの最初のビットの引数です) しかし、これはfunctionfのオブジェクトとして呼び出しているようですTk()。これは明らかに機能しません。これを回避する方法はありますか?

4

1 に答える 1

0

master は Tk() インスタンスです。parentButtonFrame クラスで変数を呼び出したからといって、それがインスタンスを作成したオブジェクトであるとは限りません。

selffromPlotAppを親として渡す必要があります (またmaster、ButtonFrame にオブジェクトが必要な場合は、マスターを in のようなものに保存しますself.master) PlotApp、またはPlotAppインスタンスのself変数をButtonFrame.

于 2013-05-06T12:21:43.123 に答える