Pergunta

I am learning wxPython. I was taught that when you use various id's on your menus such as ID_EXIT, ID_NEW, ID_UNDO etc it would put a name and ICON on the menu. But unfortunately, icons not showing in WINDOWS. And I assume that it works only in linux.

Is there any other way to do this? Where can I find some icon packs to continue my learning in wxpython? As I can't use those id icons, I wanted to set them manually with SetBitmap. But as I don't have any icons at all, I also can't do this.

Foi útil?

Solução

Here is a simple example to add images to your menu items. I have used SetBitmap() to add the images to a menu item. Download the Exit-icon.png file from here and put it in the same directory where the script resides.

import wx

class GUI(wx.Frame):
    def __init__(self, parent, id, title):
        screenWidth = 500
        screenHeight = 400
        screenSize = (screenWidth,screenHeight)
        wx.Frame.__init__(self, None, id, title, size=screenSize)
        myMenuBar = wx.MenuBar()
        file = wx.Menu()
        quit = wx.MenuItem(file, 101, '&Quit\tCtrl+Q', 'Quit the Application')
        # Adding an image to the quit menu item
        quit.SetBitmap(wx.Image('Exit-icon.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap())
        file.AppendItem(quit)
        file.AppendSeparator()
        myMenuBar.Append(file, '&File')
        self.SetMenuBar(myMenuBar)

if __name__=='__main__':
    app = wx.App(False)
    frame = GUI(parent=None, id=-1, title="Problem Demo-PSS")
    frame.Show()
    app.MainLoop()

Edit: Mr. wxPython commented regarding the icon packs:

wxPython also includes a small set of stock icons. See the docs and demos for wx.ArtProvider.

Regarding the icons pack you should prefer to use .png files and you can find a lot of free stuff on the internet. There is also a standard size format of the icons depending on the OS. For eg., I got the following table from here:

OS          Icon format Size 

Windows     16x16, 24x24, 32x32, 48x48, 256x256
Mac OS X    16x16, 32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024
Linux       16x16, 24x24, 48x48, and 96x96
iOS 6       29x29, 50x50, 57x57, 58x58, 72x72, 100x100, 114x114, 144x144, 1024x1024
iOS 7       29x29, 40x40, 58x58, 60x60, 76x76, 80x80, 120x120, 152x152, 1024x1024
Android     36x36, 48x48, 72x72, 96x96, 512x512
Windows Phone   62x62, 99x99, 173x173, 200x200

Update: You can find icon packs here for an example. Use the search option:

Exit icon http://findicons.com/search/exit

Undo icon http://findicons.com/search/undo

Redo icon http://findicons.com/search/redo

Simply click on any icon a new page opens and then click on download png button. Also you can simply rename the icon file even if it is not named a Exit/Undo/Redo. :)

Outras dicas

In wxPython 4.0.1 (and possibly earlier releases) you do not need to download icons for the basic stuff. Using zetcode's simple toolbar example the following line throws an error if 'tnew.png' is not found:

toolbar1.AddLabelTool(wx.ID_ANY, '', wx.Bitmap('tnew.png'))

however, you can use the built-in icons using wx.ArtProvider.GetBitmap():

icon = wx.ArtProvider.GetBitmap(wx.ART_QUIT)
toolbar1.AddTool(wx.ID_ANY, '', icon)  # AddLabelTool raises DeprecationWarning

and here is a list of what is embedded, ART_QUIT is probably not the best replacement 'tnew.png'.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top