Skip to main content
 首页 » 编程设计

python之wx.Image 抛出 PyAssertionError

2024年10月24日12dflying

我运行了以下 python 代码

import wx 
 
class myframe(wx.Frame): 
     
    def __init__(self): 
        wx.Frame.__init__(self, None, -1, title="Hello") 
        self.InitUI() 
 
    def InitUI(self): 
        menubar = wx.MenuBar() 
        fileMenu = wx.Menu() 
        qmi = wx.MenuItem(fileMenu, 100, '&Quit\tCtrl+Q') 
        qmi.SetBitmap(wx.Image( 
            'quit.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()) 
        fileMenu.AppendItem(qmi) 
        self.Bind(wx.EVT_MENU, self.OnQuit, id=100) 
        menubar.Append(fileMenu, '&File') 
        self.SetMenuBar(menubar) 
        self.SetSize((250, 200)) 
        self.SetTitle('Icons and shortcuts') 
        self.Centre() 
        self.Show(True) 
 
    def OnQuit(self, e): 
        self.Close() 
 
def main(): 
    ex = wx.App() 
    myframe() 
    ex.MainLoop() 
 
if __name__ == '__main__': 
    main() 

上面的代码是抛出消息

qmi.SetBitmap(wx.Image('quit.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()) 
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 2882, in __init__ 
    _core_.Image_swiginit(self,_core_.new_Image(*args, **kwargs)) 
PyAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1449) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale. 
Things are going to break, please only change locale by creating wxLocale objects to avoid this! 

我是 python 新手,完全无法解决这个问题。 有没有办法解决这个问题。

我在 Windows 机器上使用 python 2.7.10 和 wxpython 3.0.2.0。

请您参考如下方法:

我有一个类似的问题,只发生在使用 wxPhoenix 的 cxFreeze 之后。我担心上面的解决方案可能会在不是 wx.LANGUAGE_ENGLISH 的机器上引起问题,因此,我在任何导入之前记录了语言环境(我怀疑这是在更改语言环境):

import wx 
locale = wx.Locale.GetSystemLanguage() 

然后在导入后和创建应用后重置它:

app = wx.App(redirect=False) 
app.locale = wx.Locale(locale) 

这对我有用。