Start New Discussion within our Software Development Community

I was trying to create a simple Python GUI with a label on a frame. I can't get the label's position and size to work:

# a simple wxPython GUI program
# showing a label with color text
# however, label position and size do not work!

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")

        s = "Red letters on a yellow background"
        label = wx.StaticText(self, -1, label=s, pos=(20, 100), size=(200, 30))
        # set fg and bg color of label
        label.SetForegroundColour("red")
        label.SetBackgroundColour("yellow")


app = wx.App(redirect=True)
# create a frame of given size = (width, height)
title = "color label"
frame = MyFrame(None, title, (400, 240))
frame.Show(True)
app.MainLoop()

Congratulation, you discovered an idiosyncrasy of wx.Frame with just a single widget on it. My advice, use a panel to give the correct layout ...

# a simple wxPython GUI program
# showing a label with color text
# use a panel to give correct layout

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        panel = wx.Panel(self)
        panel.SetBackgroundColour("white")
        
        s = "Red letters on a yellow background"
        label = wx.StaticText(panel, -1, label=s, pos=(20, 100), size=(200, 30))
        # set fg and bg color of label
        label.SetForegroundColour("red")
        label.SetBackgroundColour("yellow")
        

app = wx.App(redirect=True)
# create a frame of given size = (width, height)
title = "color label"
frame = MyFrame(None, title, (400, 240))
frame.Show(True)
app.MainLoop()
This article has been dead for over six months. Start a new discussion instead.