尝试实例化一个 wx.HeaderCtrl 并得到一个不熟悉的错误

Trying to instantiate a wx.HeaderCtrl and getting an unfamiliar error

我正在尝试创建一个 wx.HeaderCtrl 对象,但我遇到了一个我无法在 google 上找到的错误。这是代码:

import wx

class MyApp(wx.App):
    def __init__(self):
        super().__init__()
        self.frame = MyFrame(parent=None, title="Configuration")
        self.frame.Show()

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super().__init__(parent, title=title, size=(1600, 800))
        self.configpanel = MyPanel(self)

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)

        foo = MyHeaderCtrl(self)
        foo.Create(self)

class MyHeaderCtrl(wx.HeaderCtrl):
    def __init__(self, parent):
        super().__init__(parent)

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

我的问题出在“我的面板”class 中,我在其中尝试实例化 HeaderCtrl foo,然后创建它。无论我如何组织这些,或者我将哪种 window 或面板设置为 Create 的父级,我都会收到此错误:

Traceback (most recent call last):

File "C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py", line 32, in app = MyApp()

File "C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py", line 7, in init self.frame = MyFrame(parent=None, title="Configuration")

File "C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py", line 14, in init self.configpanel = MyPanel(self)

File "C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py", line 22, in init foo = MyHeaderCtrl(self).Create(wx.Window())

wx._core.wxAssertionError: C++ assertion ""!m_hWnd"" failed at ....\src\msw\window.cpp(3971) in wxWindow::MSWCreate(): window can't be recreated

Process finished with exit code -1073741819 (0xC0000005)

这是我第一次介绍如何实现抽象 classes,并且必须使用单独的 Create() 而不仅仅是 init,所以我确定这很简单,但我很难在网上找到类似的东西。 任何帮助将不胜感激。

在 wxWidgets 中,如果您已经使用其非默认构造函数创建了 window,则不能调用 Create()。在您的代码中,您已经通过在您自己的版本中调用其 __init__ 来创建 window,因此您以后不能调用 Create() - 只需删除此行即可解决问题。