简体   繁体   English

更新旧的 wxPython 应用程序:无效的 Window 调用超级/父方法

[英]Updating Old wxPython App: Invalid Window Calling Super/Parent Method

I am trying to update an old app that was written for Python 2.7 with wxWidgets/wxPython 2.8.我正在尝试使用 wxWidgets/wxPython 2.8 更新为 Python 2.7 编写的旧应用程序 I am trying to make it compatible with my current system's versions of Python (3.10) & wxPython (4.0).我正在尝试使其与我当前系统的 Python (3.10) 和 wxPython (4.0) 版本兼容。

I have come across an error trying to call a super/parent method.我在尝试调用超级/父方法时遇到错误。 The class is derived from wx.ScrolledWindow . class 派生自wx.ScrolledWindow

The old code snippet is ( ui/panel.py starting at line 74 ):旧代码片段是(从第 74 行开始的 ui/panel.py ):

def SetScrollbars(window):
  if isinstance(window, wx.ScrolledWindow):
    window.SetScrollbars(20, 20, 0, 0)

## A wx.ScrolledWindow that sets scrollbars by default
class ScrolledPanel(wx.ScrolledWindow, PanelBase):
  def __init__(self, parent, win_id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
      style=wx.HSCROLL|wx.VSCROLL, name="scrolledPanel"):
    wx.ScrolledWindow.__init__(self, parent, win_id, pos, size, style, name)
    SetScrollbars(self)

  ## Override inherited method to also update the scrollbars
  def Layout(self):
    layout = wx.ScrolledWindow.Layout(self)
    self.UpdateScrollbars()
    return layout

The error is:错误是:

  File "ui/panel.py", line 87, in Layout
    layout = wx.ScrolledWindow.Layout(self)
wx._core.wxAssertionError: C++ assertion "m_widget" failed at ../src/gtk/window.cpp(2888) in DoSetSize(): invalid window

I searched for how to call parent/super methods in Python & found from this question that Python has a super() function .我在 Python 中搜索了如何调用父/超级方法,并从这个问题中发现 Python 有一个super() function

So I changed line 87:所以我更改了第 87 行:

-    layout = wx.ScrolledWindow.Layout(self)
+    layout = super().Layout()

The same error is reported:报同样的错误:

  File "/home/jordan/Development/Debreate/code/ui/panel.py", line 87, in Layout
    layout = super().Layout()
wx._core.wxAssertionError: C++ assertion "m_widget" failed at ../src/gtk/window.cpp(2888) in DoSetSize(): invalid window

I suppose calling the parent/super method either way is correct.我想以任何一种方式调用父/超级方法都是正确的。 So that must not be the issue.所以这一定不是问题。

My system's version of wxWidgets is 3.0.5.1.我系统的 wxWidgets 版本是 3.0.5.1。 I found the assertion check line in the wxWidgets source code .在 wxWidgets 源代码中找到了断言检查行 The checking function, wxCHECK_RET , "Checks that the condition is true, and returns if not".检查 function, wxCHECK_RET ,“检查条件是否为真,否则返回”。 So the window is invalid.所以 window 无效。 I just don't understand why as this worked with older versions of wxWidgets/wxPython.我只是不明白为什么这适用于旧版本的 wxWidgets/wxPython。

wx.ScrolledWindow is an alias for ScrolledPanel since version 2.9.0. wx.ScrolledWindow是自 2.9.0 版以来 ScrolledPanel 的别名。 In older versions, it was a standalone class.在旧版本中,它是独立的 class。
Try reading up on https://docs.wxpython.org/wx.lib.scrolledpanel.html as its replacement.尝试阅读https://docs.wxpython.org/wx.lib.scrolledpanel.html作为替代品。

As was suggesting by Rolf of Saxony, switching to wx.lib.scrolledpanel.ScrolledPanel solves the problem:正如萨克森的 Rolf 所建议的,切换到wx.lib.scrolledpanel.ScrolledPanel可以解决问题:

from wx.lib.scrolledpanel import ScrolledPanel as sp

class ScrolledPanel(sp, PanelBase):
  def __init__(self, parent, win_id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize,
      style=wx.HSCROLL|wx.VSCROLL, name="scrolledPanel"):
    sp.__init__(self, parent, win_id, pos, size, style, name)
    sp.SetupScrolling(self)

But I also want to note that I was able to work around the problem by calling Layout on the parent window:但我还想指出,我可以通过在父 window 上调用Layout来解决该问题:

  ## Override inherited method to also update the scrollbars
  def Layout(self):
    layout = self.GetParent().Layout(self)
    self.UpdateScrollbars()
    return layout

But using wx.lib.scrolledpanel.ScrolledPanel is probably the proper way to upgrade.但是使用wx.lib.scrolledpanel.ScrolledPanel可能是升级的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM