【问题标题】:python call method from different class来自不同类的python调用方法
【发布时间】:2015-08-04 14:21:24
【问题描述】:

我正在尝试从另一个类 (prefWindow) 调用驻留在一个类 (Main) 中的方法 (OnOpen)。我正在使用 wx python 作为 GUI。 问题是每当我尝试使用这个 parent.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)

谢谢。

【问题讨论】:

  • 您的代码 sn-p 中也缺少一些东西...

标签: python class wxwidgets


【解决方案1】:

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

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.Frame 的所有方法都可以在MainprefWindow 的实例上使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多