Guys I am trying to add an image to the window I created, but I get an error message when I run my code:

import wx
       
class my_window(wx.Frame):
    app=wx.PySimpleApp()
      
    def __init__(self, parent, id):
        window = wx.Frame.__init__(self,parent,id,'Window', size=(600,400))
        window.SetIcon(wx.Icon('porcupine.ico', wx.BITMAP_TYPE_ICO))
       
if __name__ == "__main__":
      frame=my_window(parent=None,id=-1)
      frame.Center()
      frame.Show()
      app.MainLoop()

The message I get:

Traceback (most recent call last):
  File "wxpython2.py", line 20, in <module>
    frame=my_window(parent=None,id=-1)
  File "wxpython2.py", line 17, in __init__
    window.SetIcon(wx.Icon('porcupine.ico', wx.BITMAP_TYPE_ICO))
AttributeError: 'NoneType' object has no attribute 'SetIcon'

Recommended Answers

All 5 Replies

I got it to work......kinda:
It opens a window but it's just gray. :(

import wx
       
class my_window(wx.Frame):
      
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id,'Window', size=(600,400))
        panel = wx.Panel(self)
        pic = wx.Icon("porcupine.ico", wx.BITMAP_TYPE_ICO)
        self.SetIcon(pic)
       
if __name__ == "__main__":
      app=wx.PySimpleApp()
      frame=my_window(parent=None,id=-1)
      frame.Center()
      frame.Show()
      app.MainLoop()

because seticon only set icon for your frame that is the little icon on the bar far left.

What are you trying to achieve?

;)

I am just trying to add an image to my window lol.

What do you mean:
"seticon only set icon for your frame that is the little icon on the bar far left"

I didn't understand that.

This is my new code:

import wx
       
class my_window(wx.Frame):
      
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id,'Window', size=(600,400))
        panel = wx.Panel(self)
        pic = wx.Icon("porcupine.ico", wx.BITMAP_TYPE_ICO)
        SetImage(self, pic)
       
if __name__ == "__main__":
      app=wx.PySimpleApp()
      frame=my_window(parent=None,id=-1)
      frame.Center()
      frame.Show()
      app.MainLoop()

To show icon on the top bar and the panel.....
use this

import wx

class Fra(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent)
        
        self.ico=wx.IconFromBitmap(wx.Bitmap("/home/richie/icon.xpm"))#path to icon
        self.SetIcon(self.ico)
        
        self.Show()
ap=wx.App()
Fra(None)
ap.MainLoop()

Just change the path to your icon

###############################################################################
# To set a bitmap on your frame use this.

import wx

class Fra(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent)
        
        pan=wx.Panel(self,-1)
        pic=wx.StaticBitmap(pan)
        pic.SetBitmap(wx.Bitmap("/home/richie/o.png"))
        
        self.Show()
ap=wx.App()
Fra(None)
ap.MainLoop()

They are to give you the idea how powerful wxpython is...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.