wxPython菜单不显示图像 [英] wxPython menu doesn't display image

查看:277
本文介绍了wxPython菜单不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a menu and assigning images to menu items, sometime first item in menu doesn't display any image, I am not able to find the reason. I have tried to make a simple stand alone example and below is the code which does demonstrates the problem on my machine. I am using 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()

那么您能够看到问题了,可能是什么原因造成的?

So are you able to see the problem and what could be the reason for it?

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

Conclusion: Yes this is a official bug, I have created a simple Menu class to overcome this bug, using the trick given by "balpha" in selected answer

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

It overrides each menu.Append method and sees if menu item with image is being added for first time, if yes creates a dummy item and deletes it later.

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

This also adds feature/constraint so that instead of calling SetBitmap, you should pass bitmap as optional argument image

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)

推荐答案

这是一个确认的错误似乎已经开放了很长一段时间.经过一番尝试后,似乎可以解决此问题:

This is a confirmed bug which appearently has been open for quite a while. After trying around a little bit, this workaround seems to do it:

    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.

Note that the position of the fileMenu.Remove call is the earliest position that works, but you can also move it to the bottom. HTH.

这篇关于wxPython菜单不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆