Guys, I am trying to change the background color of my window in WxPython.
Here is what I got:
Why doesn't it work?

import wx

class my_window(wx.Frame):
    
    def __int__(self, parent, id):
        wx.Frame.__int__(self,parent,id,'My Window', size=(300,200))
        self.Colours()

    
    def Colours(self):
        self.pnl1.SetBackgroundColour(wx.BLACK)
        


if __name__ == "__main__":
    app=wx.PySimpleApp()
    frame=my_window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

Recommended Answers

It work fine for me,it has no error so it should work for you to.
In your first code both [B]int[/B] statement was wrong should be [B]init[/B]
[CODE]import wx

class my_window(wx.Frame):
def init(self, parent, id):
wx.Frame.init(self,parent,id,'My Window', size=(300,200))

    self.SetBackgroundColour('BLACK')

if name == "main":
app=wx.PySimpleApp()
frame=my_window(parent=None,id=-1)
frame.Show()
app.MainLoop()[/CODE]

Jump to Post

All 5 Replies

Re: WxPython Changing Background Color 80 80
import wx

class my_window(wx.Frame):
    def __init__(self, parent, id):  #error __int__
        wx.Frame.__init__(self,parent,id,'My Window', size=(300,200))

        #Panel for frame
        self.panel = wx.Panel(self)
        self.SetBackgroundColour('blue')

if __name__ == "__main__":
    app=wx.PySimpleApp()
    frame=my_window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
Re: WxPython Changing Background Color 80 80
import wx

class my_window(wx.Frame):
    def __init__(self, parent, id):  #error __int__
        wx.Frame.__init__(self,parent,id,'My Window', size=(300,200))

        #Panel for frame
        self.panel = wx.Panel(self)
        self.SetBackgroundColour('blue')

if __name__ == "__main__":
    app=wx.PySimpleApp()
    frame=my_window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

snippsat when I ran your code I got the same result with yours that I did with mine.
Just a gray window pops up.

Re: WxPython Changing Background Color 80 80

It work fine for me,it has no error so it should work for you to.
In your first code both __int__ statement was wrong should be __init__

import wx

class my_window(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id,'My Window', size=(300,200))
    
        self.SetBackgroundColour('BLACK')

if __name__ == "__main__":
    app=wx.PySimpleApp()
    frame=my_window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
Re: WxPython Changing Background Color 80 80

Thanks snippsat I got it to work.
It wouldn't work when I ran it with:

#Panel for frame
self.panel = wx.Panel(self)

and also __int__ instead of __init__ like you said :)


Here is my final code:

import wx
       
class my_window(wx.Frame):
      
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id,'My Window', size=(300,200))
        self.SetBackgroundColour(wx.BLUE)
       
if __name__ == "__main__":
      app=wx.PySimpleApp()
      frame=my_window(parent=None,id=-1)
      frame.Show()
      app.MainLoop()
Re: WxPython Changing Background Color 80 80

When you added the panel, you didn't call SetBackgroundColour on the panel, but you were still using the Frame's method. Instead of:

    #Panel for frame
    self.panel = wx.Panel(self)
    self.SetBackgroundColour('blue')

It should be:

    #Panel for frame
    self.panel = wx.Panel(self)
    self.panel.SetBackgroundColour('blue')
Be a part of the DaniWeb community

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