Iancovici
Iancovici

Reputation: 5731

How to control the background color of a square of a panel on wxPython?

So I'm writing a code from scratch, and I am down to the part of placing a static text (nonchanging) with a grey background. I can change the font with .ForegroundColour but not with BackgroundColour

Here's the code

s_text2 = wx.StaticText(self.panel1, -1, "\n\n\nStop\n\n\n", (x1size+30,10)) 
s_text2.SetBackgroundColour('grey')

Any thoughts?

Yeah, here is a short to the point sample

import wx

class Prototype(wx.Frame):
     def __init__(self, parent, title):
        wx.Frame.__init__(self, None, size=(550,300))

        self.InitUI()
        self.Centre()
        self.Show()
    #define User Interface
    def InitUI(self):
        self.panel1 = wx.Panel(self, -1)
        self.sizer = wx.BoxSizer() #Main window sizer
        self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)

        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.panel1.SetSizer(self.hbox)

        #Static Text
        s_text1 = wx.StaticText(self.panel1, -1, "Hello World!", (10,5)) #top text
        self.s_text2 = wx.StaticText(self.panel1, -1, "\n\n\nStop\n\n\n", (300,10)) #top text
        self.s_text2.SetBackgroundColour("blue")
if __name__ == '__main__':
    app = wx.App()
    Prototype(None, title='')
    app.MainLoop()

`

Upvotes: 0

Views: 2768

Answers (3)

Iancovici
Iancovici

Reputation: 5731

I just solved it, thx anyways guys for trying

import wx

class Prototype(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, None, size=(550,300))

        self.InitUI()
        self.Centre()
        self.Show()
    #define User Interface
    def InitUI(self):
        self.panel1 = wx.Panel(self, -1)

        #Static Text
        s_text1 = wx.StaticText(self.panel1, -1, "Hello World!", (10,5)) #top text
        self.s_text2 = wx.TextCtrl(self.panel1, style=wx.TE_READONLY | wx.NO_BORDER, pos=(300,10)) #top text
        self.s_text2.AppendText("gray")
        self.s_text2.SetBackgroundColour("gray")
if __name__ == '__main__':
    app = wx.App()
    Prototype(None, title='')
    app.MainLoop()

Upvotes: 0

RobinDunn
RobinDunn

Reputation: 6206

If you are using the wxGTK port then static texts may not be able to have background colors because they are not a true widget. Instead they are just drawn directly on the parent and the active theme can have some influence over whether the background color will be used or not.

There is a generic class in wx.lib.stattext that is a true widget and so you can use it instead.

Upvotes: 0

Fenikso
Fenikso

Reputation: 9451

This works for me:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.text = wx.StaticText(self.panel, label="Test")
        self.text.SetBackgroundColour("gray")
        self.text.SetForegroundColour(wx.WHITE)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.text)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

Upvotes: 1

Related Questions