wxPython 错误 - 部分初始化的模块“wx”没有属性“Frame”很可能是由于循环导入

问题描述

我正在关注一个使用 wxPython 的小型 Python 程序示例,如下所示:

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None,title='Hello World')
        panel = wx.Panel(self)

        self.text_ctrl = wx.TextCtrl(panel,pos=(5,5))
        my_btn = wx.Button(panel,label='Press Me',55))

        self.Show()

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

但它返回一个错误

Traceback (most recent call last):
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py",line 1,in <module>
    import wx
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py",line 3,in <module>
    class MyFrame(wx.Frame):    
AttributeError: partially initialized module 'wx' has no attribute 'Frame' (most likely due to a circular import)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py",line 72,in apport_excepthook
    from apport.fileutils import likely_packaged,get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py",line 5,in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py",line 15,in <module>
    import xml.dom,xml.dom.minidom
  File "/home/float/.local/lib/python3.8/site-packages/wx/xml.py",line 13,in <module>
    from ._xml import *
ImportError: attempted relative import with no kNown parent package

Original exception was:
Traceback (most recent call last):
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py",in <module>
    class MyFrame(wx.Frame):    
AttributeError: partially initialized module 'wx' has no attribute 'Frame' (most likely due to a circular import)

我在我的程序中找不到任何“循环导入”,所以我假设这个错误来自 wxPython 库。这个假设正确吗?我该如何解决

我在 WSL2 (Ubuntu 20) 上运行这个

编辑:问题已通过运行“pip3 install --upgrade --force-reinstall wxPython”解决,但如果可能的话,我想首先知道为什么会发生这种情况。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)