[英]Updating Old wxPython App: Invalid Window Calling Super/Parent Method
我正在嘗試使用 wxWidgets/wxPython 2.8 更新為 Python 2.7 編寫的舊應用程序。 我正在嘗試使其與我當前系統的 Python (3.10) 和 wxPython (4.0) 版本兼容。
我在嘗試調用超級/父方法時遇到錯誤。 class 派生自wx.ScrolledWindow 。
舊代碼片段是(從第 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
錯誤是:
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
我在 Python 中搜索了如何調用父/超級方法,並從這個問題中發現 Python 有一個super() function 。
所以我更改了第 87 行:
- layout = wx.ScrolledWindow.Layout(self)
+ layout = super().Layout()
報同樣的錯誤:
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
我想以任何一種方式調用父/超級方法都是正確的。 所以這一定不是問題。
我系統的 wxWidgets 版本是 3.0.5.1。 我在 wxWidgets 源代碼中找到了斷言檢查行。 檢查 function, wxCHECK_RET ,“檢查條件是否為真,否則返回”。 所以 window 無效。 我只是不明白為什么這適用於舊版本的 wxWidgets/wxPython。
wx.ScrolledWindow
是自 2.9.0 版以來 ScrolledPanel 的別名。 在舊版本中,它是獨立的 class。
嘗試閱讀https://docs.wxpython.org/wx.lib.scrolledpanel.html作為替代品。
正如薩克森的 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)
但我還想指出,我可以通過在父 window 上調用Layout
來解決該問題:
## Override inherited method to also update the scrollbars
def Layout(self):
layout = self.GetParent().Layout(self)
self.UpdateScrollbars()
return layout
但是使用wx.lib.scrolledpanel.ScrolledPanel
可能是升級的正確方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.