【问题标题】:wxPython toolbar issuewxPython工具栏问题
【发布时间】:2014-04-03 23:16:13
【问题描述】:

我对这段代码有疑问:

import wx
class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 

        self.InitUI()

    def InitUI(self):    

        toolbar = self.CreateToolBar()
        qtool = toolbar.AddLabelTool(wx.ID_ANY, 'Quit', wx.Bitmap('texit.png'))
        toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)

        self.SetSize((250, 200))
        self.SetTitle('Simple toolbar')
        self.Centre()
        self.Show(True)



    def OnQuit(self, e):
        self.Close()

def main():

    ex = wx.App()
    Example(None)
    ex.MainLoop()    


if __name__ == '__main__':
    main()

当我运行它时,我得到:

Traceback (most recent call last):
  File "...\maintest.py", line 34, in <module>
    main()
  File "...\painter3D\maintest.py", line 29, in main
    Example(None)
  File "...\maintest.py", line 8, in __init__
    self.InitUI()
  File "...\maintest.py", line 14, in InitUI
    toolbar.Realize()
  File "C:\Python27\lib\site-packages\wx-2.9.4-msw\wx\_controls.py", line 3797, in Realize
    return _controls_.ToolBarBase_Realize(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "Assert failure" failed at ..\..\src\msw\toolbar.cpp(796) in wxToolBar::Realize(): invalid tool button bitmap

我用:

Win7
Python2.7
wxPython2.8 unicode

我将不胜感激。

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    您的脚本找不到名为texit.png 的图像。

    首先,在 wxPython 库安装目录中找到该图像。提示:尝试在C:\Python27\Lib\site-packages\wx-3.0-msw\wx\tools\Editra\pixmaps\theme\Tango\menu 中找到一个名为quit.png 的文件。

    然后,将该图像复制到与您的脚本相同的位置,或者将一些 Python 代码添加到您的脚本中以指定图像位置的路径。

    我在 Windows 7 上使用 Python 2.7 64 位和 wxPython 3.0。我在你的代码上遵循了这个解决方案,它对我来说很好。

    【讨论】:

      【解决方案2】:

      试试:

      import wx
      class Example(wx.Frame):
      
          def __init__(self, *args, **kwargs):
              super(Example, self).__init__(*args, **kwargs) 
      
              self.InitUI()
      
          def InitUI(self):    
      
              toolbar = self.CreateToolBar()
              myimage = wx.Image('texit.png')
              qtool = toolbar.AddLabelTool(wx.ID_ANY, 'Quit', wx.BitmapFromImage(myimage))
              toolbar.Realize()
      
              self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
      
              self.SetSize((250, 200))
              self.SetTitle('Simple toolbar')
              self.Centre()
              self.Show(True)
      
      
      
          def OnQuit(self, e):
              self.Close()
      
      def main():
      
          ex = wx.App()
          Example(None)
          ex.MainLoop()    
      
      
      if __name__ == '__main__':
          main()
      

      也许这可以帮助你:D

      【讨论】: