wxPython AND PyGame: IMAGES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tharden3
    Contributor
    • Jul 2008
    • 916

    wxPython AND PyGame: IMAGES

    Ok, I've been going through these tutorials for both PyGame and wxPython, but I've hit a big speed bump. In each tutorial, I've gotten to the point where I need to "import" or "load" images for various purposes (animation, icons, etc.). Each time I follow the tutorial, I can't quite get it right... please don't tell me "Hey, you can't use the pictures in the tutorial example because those are not the directories, paths, or pics that are on your computer". I know that already. Help me to figure out (please provide code and examples in your posts) how to use images on my computer. I will provide the code in the tutorials, links to the tutorials, and the error I get from my code. This might require you to answer with some depth and instruction, but it would be a big help to me and my progress in learning python!

    P.S.
    - When I show my code, it is not the only thing I tried. Don't just tell me I'm wrong, provide a clear answer.
    - It is basically the same problem in wxPython and PyGame... it's all about making images work in these apps

    I've been studying and working very hard in python, and this has really stopped me dead in my tracks. Please provide whatever help you can.

    What the tutorial says to do:

    Code:
    #!/usr/bin/python
    
    # menuexample.py
    
    import wx
    
    class MenuExample(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(250, 150))
    
            menubar = wx.MenuBar()
            file = wx.Menu()
            quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
            quit.SetBitmap(wx.Bitmap('icons/exit.png'))
            file.AppendItem(quit)
    
            self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
    
            menubar.Append(file, '&File')
            self.SetMenuBar(menubar)
    
            self.Centre()
            self.Show(True)
    
        def OnQuit(self, event):
            self.Close()
    
    app = wx.App()
    MenuExample(None, -1, '')
    app.MainLoop()
    What I tried (but not the only thing I tried):

    Code:
    #!/usr/bin/python
    
    # menuexample.py
    
    import wx
    
    class MenuExample(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(250, 150))
    
            menubar = wx.MenuBar()
            file = wx.Menu()
            quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
            quit.SetBitmap(wx.Bitmap('C:\Python24\tony.bmp'))
            file.AppendItem(quit)
    
            self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
    
            menubar.Append(file, '&File')
            self.SetMenuBar(menubar)
    
            self.Centre()
            self.Show(True)
    
        def OnQuit(self, event):
            self.Close()
    
    app = wx.App()
    MenuExample(None, -1, '')
    app.MainLoop()
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by tharden3
    Code:
    #!/usr/bin/python
    # menuexample.py
    
    import wx
    
    class MenuExample(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(250, 150))
    
            menubar = wx.MenuBar()
            file = wx.Menu()
            quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
            quit.SetBitmap(wx.Bitmap('C:\Python24\tony.bmp'))
            file.AppendItem(quit)
    
            self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
    
            menubar.Append(file, '&File')
            self.SetMenuBar(menubar)
    
            self.Centre()
            self.Show(True)
    
        def OnQuit(self, event):
            self.Close()
    
    app = wx.App()
    MenuExample(None, -1, '')
    app.MainLoop()
    I've gotten bitmaps to work before in wxPython, however I've never attempted such an act in PyGame. In order to get a bitmap into a wx-friendly format I used the following:
    [code=python]
    bmp = wx.Image('Splas h.bmp', wx.BITMAP_TYPE_ BMP, -1).ConvertToBit map()
    [/code]
    And then in your code you would use
    [code=python]
    quit.SetBitmap( bmp)
    [/code]
    Please let us know if that helped at all... there are many things about bitmaps that make image handling touchy. Most of the problems you'll face revolve around how Python recognizes the image file. That's why I needed to specify the type of the image with wx.BITMAP_TYPE_ BMP

    Comment

    • tharden3
      Contributor
      • Jul 2008
      • 916

      #3
      Originally posted by jlm699
      I've gotten bitmaps to work before in wxPython, however I've never attempted such an act in PyGame. In order to get a bitmap into a wx-friendly format I used the following:
      [code=python]
      bmp = wx.Image('Splas h.bmp', wx.BITMAP_TYPE_ BMP, -1).ConvertToBit map()
      [/code]
      And then in your code you would use
      [code=python]
      quit.SetBitmap( bmp)
      [/code]
      Please let us know if that helped at all... there are many things about bitmaps that make image handling touchy. Most of the problems you'll face revolve around how Python recognizes the image file. That's why I needed to specify the type of the image with wx.BITMAP_TYPE_ BMP
      Thanks for the help. I did end up getting this to work (in wxPython, not pygame), but with PNG and JPEG files. But I'll be able to use this in the future.

      Comment

      Working...