问题
I am trying to set the backgroundcolor of a StaticText in wxPython. I am using the method
.SetBackgroundColour('Red')
. That works as expected when I run my code on windows 10. However, when it runs on a Raspberry pi it fails to set the background colour (and the text alignment). Can someone help?
Picture: Result on windows 10
Picture: Result on Raspberry pi 3b+
The code and a wheel file built on the raspberry for wxPython4.0.3 can be found on my Github: https://github.com/danneedebro/Problem_bgcolor_static_text
This is my simplified code:
Main.py
import wx
class Example(wx.Frame):
def __init__(self, parent):
super(Example, self).__init__(parent, title='Window', size=(300, 100))
panel = wx.Panel(self)
LblTextWithBgColor = wx.StaticText(panel, label='This text should have a red background', size=(250, 20), style=wx.ALIGN_CENTER)
LblTextWithBgColor.SetForegroundColour('Green')
LblTextWithBgColor.SetBackgroundColour('Red')
self.Show()
app = wx.App()
Example(None)
app.MainLoop()
回答1:
You cannot set the background color of wx.StaticText
in GTK. The label is just drawn on the parent window, which prevents you from doing much of anything with the widget. This is documented in a non-obvious location:
- https://wxpython.org/Phoenix/docs/html/wx.lib.stattext.html
However you can use the wx.lib.stattext
instead as it is a generic widget that should work the same way on all platforms.
来源:https://stackoverflow.com/questions/53630830/fails-to-set-bgcolor-on-statictext-on-raspberry-pi-but-not-in-win10