【问题标题】:Trying to instantiate a wx.HeaderCtrl and getting an unfamiliar error试图实例化一个 wx.HeaderCtrl 并得到一个不熟悉的错误
【发布时间】:2020-03-06 23:13:56
【问题描述】:

我正在尝试创建一个 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()

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

Traceback(最近一次调用最后一次):

文件“C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py”,行 32,在 app = MyApp()

文件“C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py”,行 7、在初始化 self.frame = MyFrame(parent=None, title="配置")

文件“C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py”,行 14、在初始化 self.configpanel = MyPanel(self)

文件“C:/_Code/Projects/Personal/BigOlTimeline/Python/test.py”,行 22、在初始化 foo = MyHeaderCtrl(self).Create(wx.Window())

wx._core.wxAssertionError: C++ 断言 ""!m_hWnd"" 失败于 ....\src\msw\window.cpp(3971) in wxWindow::MSWCreate(): window can't 重新创建

进程以退出代码 -1073741819 (0xC0000005) 结束

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

【问题讨论】:

  • 这是您展示的完整代码吗?看起来您已经创建了控件并尝试再次创建对象的相同实例...
  • 是的,这是一个重新创建错误的工作程序。我同意你接受该函数的文档,说我必须在实例化它之后运行 Create。 "init (self) 默认构造函数不创建底层窗口。您必须在使用此构造函数创建对象后使用 Create。"
  • wxPython 可能不支持 2 路窗口创建。
  • 这是从 wxpython 文档中提取的,但说它没有,我该怎么做?
  • 看wxpython demo和样例

标签: python wxpython wxwidgets


【解决方案1】:

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

【讨论】:

  • 啊,这正是我想要的,我确定我只是误解了功能。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2018-03-14
  • 2014-08-04
相关资源
最近更新 更多