ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

wxPython菜单不显示图像

2019-08-27 17:56:40  阅读:179  来源: 互联网

标签:python menu wxpython


我正在创建菜单并将图像分配给菜单项,有时菜单中的第一项不显示任何图像,我无法找到原因.我试图制作一个简单的独立示例,下面的代码确实演示了我的机器上的问题.
我使用的是Windows XP,wx 2.8.7.1(msw-unicode)’

import wx

def getBmp():
    bmp = wx.EmptyBitmap(16,16)
    return bmp

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, style=wx.DEFAULT_FRAME_STYLE, parent=None)

        self.SetTitle("why New has no image?")

        menuBar = wx.MenuBar()
        fileMenu=wx.Menu()
        item = fileMenu.Append(wx.ID_NEW, "New")
        item.SetBitmap(getBmp())
        item = fileMenu.Append(wx.ID_OPEN, "Open")
        item.SetBitmap(getBmp())
        item = fileMenu.Append(wx.ID_SAVE, "Save")
        item.SetBitmap(getBmp())
        menuBar.Append(fileMenu, "File")
        self.SetMenuBar(menuBar) 


app = wx.PySimpleApp()
frame=MyFrame()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()

那么你能看到问题吗?它可能是什么原因?

结论:是的,这是一个官方错误,我创建了一个简单的Menu类来克服这个错误,使用选定答案中“balpha”给出的技巧

它会覆盖每个menu.Append方法并查看是否第一次添加带有图像的菜单项,如果是,则创建一个虚拟项并稍后将其删除.

这也添加了功能/约束,因此您应该将位图作为可选参数图像传递,而不是调用SetBitmap

import wx

class MockMenu(wx.Menu):
    """
    A custom menu class in which image param can be passed to each Append method
    it also takes care of bug http://trac.wxwidgets.org/ticket/4011
    """

    def __init__(self, *args, **kwargs):
        wx.Menu.__init__(self, *args, **kwargs)
        self._count = 0

    def applyBmp(self, unboundMethod, *args, **kwargs):
        """
        there is a bug in wxPython so that it will not display first item bitmap
        http://trac.wxwidgets.org/ticket/4011
        so we keep track and add a dummy before it and delete it after words
        may not work if menu has only one item
        """

        bmp = None
        if 'image' in kwargs:
            bmp = kwargs['image']

        tempitem = None
        # add temp item so it is first item with bmp 
        if bmp and self._count == 1:
            tempitem = wx.Menu.Append(self, -1,"HACK")
            tempitem.SetBitmap(bmp)

        ret = unboundMethod(self, *args, **kwargs)
        if bmp:
            ret.SetBitmap(bmp)

        # delete temp item
        if tempitem is not None:
            self.Remove(tempitem.GetId())

        self._lastRet = ret
        return ret

    def Append(self, *args, **kwargs):
        return self.applyBmp(wx.Menu.Append, *args, **kwargs)

    def AppendCheckItem(self, *args, **kwargs):
        return self.applyBmp(wx.Menu.AppendCheckItem, *args, **kwargs)

    def AppendMenu(self, *args, **kwargs):
        return self.applyBmp(wx.Menu.AppendMenu, *args, **kwargs)

解决方法:

这是confirmed bug,显然已经开放了很长一段时间.尝试了一下后,这个解决方法似乎做到了:

    menuBar = wx.MenuBar()
    fileMenu=wx.Menu()
    tempitem = fileMenu.Append(-1,"X")       # !!!
    tempitem.SetBitmap(getBmp())             # !!!
    item = fileMenu.Append(wx.ID_NEW, "New")
    fileMenu.Remove(tempitem.GetId())        # !!!
    item.SetBitmap(getBmp())
    item = fileMenu.Append(wx.ID_OPEN, "Open")
    item.SetBitmap(getBmp())
    item = fileMenu.Append(wx.ID_SAVE, "Save")
    item.SetBitmap(getBmp())
    menuBar.Append(fileMenu, "File")
    self.SetMenuBar(menuBar) 

请注意,fileMenu.Remove调用的位置是最有效的位置,但您也可以将其移动到底部. HTH.

标签:python,menu,wxpython
来源: https://codeday.me/bug/20190827/1743244.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有