Reputation: 1129
[MacOS Catalina]
I'm a complete noob to wxPython. I'm learning to use wxPython via a variety of online tutorials, but I'm having a bit of an issue I can't find an answer to.
Since I'm using 'Dark Mode' on my Mac, the label text of buttons is rendered white. This makes them hard to read and I would prefer the text to be black. However, I haven't been able to find anything that explains how to do this with a wx.button. The closest answer I've been able to find suggests that it is possible to set System Options using something like SetOptions(button_color=sg.COLOR_SYSTEM_DEFAULT)
, however I can't figure out what object to apply the setting to at the moment.
Is there a way of changing the colour of the label
property of a wx.button
?
My code is super simple:
import wx
class MyFrame(wx.Frame):
def __init__(self):
super(wx.Frame, self).__init__(parent=None, title='Hello World')
panel = wx.Panel(self)
# wx.SetOptions(button_color=sg.COLOR_SYSTEM_DEFAULT)
self.text_ctrl = wx.TextCtrl(panel, pos=(5, 5))
self.text_ctrl.SetBackgroundColour((255, 255, 255, 255))
font = wx.Font(10, family = wx.FONTFAMILY_MODERN, style = 0, weight = 90,
underline = False, faceName ="", encoding = x.FONTENCODING_DEFAULT)
my_btn = wx.Button(panel, label='Press Me', pos=(5, 55))
my_btn.SetFont(font)
my_btn.SetBackgroundColour((255, 0, 0, 255))
my_btn.SetForegroundColour((255, 0, 0, 255))
# my_btn.SetDefaultStyle(wx.TextAttr(wx.RED))
# my_btn.AppendText("Red text\n")
# my_btn.SetDefaultStyle(wx.TextAttr(wx.NullColour, wx.LIGHT_GREY))
# my_btn.AppendText("Red on grey text\n")
# my_btn.SetDefaultStyle(wx.TextAttr(wx.BLUE))
# my_btn.AppendText("Blue on grey text\n")
self.Show()
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
app.MainLoop()
The commented out lines indicate some of the things I've tried thus far.
Edit The answer below works for Python3, but using Python2, I'm getting the following result:
Upvotes: 1
Views: 1318
Reputation: 22433
Use the SetOwnForegroundColour
of the button e.g.
import wx
class MainFream(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None,title='test',size=(500,500), style=wx.DEFAULT_FRAME_STYLE)
self.MainPanel = wx.Panel(self)
test = []
button_id = []
for i in range(1,21):
test.append(i)
button_id.append(wx.NewId())
self.button = []
for i in range(len(test)):
self.button.append(wx.Button(self.MainPanel,button_id[i],label=(str(test[i]))))
self.button[i].Bind(wx.EVT_BUTTON, self.OnButton)
self.button[2].SetOwnForegroundColour(wx.RED)
self.button[9].SetOwnForegroundColour(wx.GREEN)
self.button[12].SetOwnForegroundColour("brown")
self.button[16].SetOwnForegroundColour("cadetblue")
sizer = wx.FlexGridSizer(0, 5, 5, 5)
for i in self.button:
sizer.Add(i, 0, wx.ALL, 0)
self.MainPanel.SetSizer(sizer)
def OnButton(self, event):
Id = event.GetId()
Obj = event.GetEventObject()
print ("Button Id",Id)
print ("Button Pressed:",Obj.GetLabelText())
if __name__ == '__main__':
app = wx.App()
fream = MainFream()
fream.Show()
app.MainLoop()
Upvotes: 1