来自不同类的python调用方法

2024-04-23 22:03:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从另一个类(prefWindow)调用驻留在一个类(Main)中的方法(OnOpen)。我使用wxpython作为GUI。 问题是每当我尝试使用这个父.OnOpen,它错误地说它没有定义。 我绝不是python专家,只是最近才开始的。你知道吗

class Main(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Main, self).__init__(*args, **kwargs)
        self.initUI()

    def initUI(self):
       *more code here*

    def OnOpen(self,e): 
      global dirname
      dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.apk", wx.OPEN) #open the dialog box to open file

class prefWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id)  
        self.initPref()

    def initPref(self):
       browseBtn = wx.Button(panel, -1, "Browse")
       self.Bind(wx.EVT_BUTTON, parent.OnOpen, browseBtn)

谢谢。你知道吗


Tags: selfinitmaindefargsopenframekwargs
1条回答
网友
1楼 · 发布于 2024-04-23 22:03:28

使用类继承并创建自己的基类:

class MyWindow(wx.Frame):
    def OnOpen(self,e): 
      global dirname
      dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.apk", wx.OPEN) #open the dialog box to open file

class Main(MyWindow):
    ...

class prefWindow(MyWindow):
    ...

现在onOpen()方法以及wx.FrameMainprefWindow实例的所有方法都将可用。你知道吗

相关问题 更多 >