使用wx.frame进行Python类继承

问题描述 投票:0回答:1

我使用python2.7 + wxpython来显示一些图表数据。

我有两个类,一个(wx类)管理wx.frame而另一个(文本解码类)用于文件上下文的解析器。我可以使用self。*在wx类中控制wx.frame(例如:self.SetMenuBar,self.SetTitle),我可以将这个“self”传递给另一个类来控制相同的wx.frame吗?

喜欢关注简要代码,

class CanvasFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
        self.SetBackgroundColour(wxc.NamedColour("WHITE"))
        ....
        normal_test_mode_decode(filename, directory)
        ....

class normal_test_mode_decode(CanvasFrame):
    def __init(self, csv_fname, csv_dir):
        ....
        self.SetTitle(os.path.join(csv_dir, csv_fname)) #Error here

class MyApp(wx.App):
    def OnInit(self):
        frame = CanvasFrame(None, "wxPython mathtext demo app")
        ....

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

如果我直接在另一个类使用self.SetTitle,这是错误消息。

Traceback (most recent call last):
  File "D:\normal_test_mode_parser\normal_test_mode_parser.py", line 45, in OnOpen
    self.decode = normal_test_mode_decode(filename, directory)
  File "D:\normal_test_mode_parser\normal_test_mode_parser.py", line 64, in __init__
    self.SetTitle(os.path.join(csv_dir, csv_fname))
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 455, in SetTitle
    return _windows_.TopLevelWindow_SetTitle(*args, **kwargs)
TypeError: in method 'TopLevelWindow_SetTitle', expected argument 1 of type 'wxTopLevelWindow *'

我想解决这个问题,因为我的计划是wxpython用于管理GUI功能的类(CanvasFrame)和另一个类(normal_test_mode_decode)处理数据分析功能。

谢谢!

运行此代码时,选择文件 - >打开任何文件,然后您会看到错误。

python-2.7 inheritance wxpython
1个回答
0
投票

我对您的代码进行了一些小的更改,以简化它或消除错误。

  • 我用super替换了涉及super()的更复杂的表达式,因为你使用了Py3,这使得错误不太可能发生。
  • super().__init__(parent, id=-1, title='something something', size=(550, 350)),我添加了字面标题。这可能不是您最终想要的,但它可以继续开发工作。
  • 我将self.content =附近的线条更改为self.content = self.fp.readline().strip().split(','),因为line未定义。同样,您可能希望给予更多关注。
© www.soinside.com 2019 - 2024. All rights reserved.