Search code examples
pythonwxpythonwildcard

How to get png or jpeg or bmp pictures in wxpython


I want to search for png, jpeg and bmp pictures but it doesn't seems to work.

def onClick(self,Event):
        wildcard='PNG files (*.png)|*.png|'|'BMP files (*.bmp)|*.bmp|'|'JPEG files (*.jpg)|*.jpg|'
        openFileDialog = wx.FileDialog(self, "Open", "", "", wildcard,wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()

Here is the error and i don't know how to resolve it

wildcard='PNG files (*.png)|*.png|'|'BMP files (*.bmp)|*.bmp|'|'JPEG files (*.jpg)|*.jpg|'
TypeError: unsupported operand type(s) for |: 'str' and 'str'

Solution

  • Doing a string | string does not make sense. That means that

    'something'|'something'
    

    throws the exception above. All of your | characters should be a part of the wildcard string.

    For just pngs use:

    'PNGs (*.png)|*.png'
    

    For gifs and jpegs use:

    'Various image formats - png,jpg|*.png;*.jpg'
    

    If you want to let the user choose:

    'Various image formats|*.png;*.jpg|Just BMPs (*.bmp)|*.bmp'