是否可以更改 wx.Button 的标签文本颜色
Is it possible to change wx.Button's label text colour
[MacOS卡特琳娜]
我完全是 wxPython 的菜鸟。我正在通过各种在线教程学习使用 wxPython,但我遇到了一些问题,我找不到答案。
因为我在 Mac 上使用 'Dark Mode',按钮的标签文本呈现为白色。这使它们难以阅读,我希望文本是黑色的。但是,我无法找到任何解释如何使用 wx.button 执行此操作的内容。我能找到的最接近的答案表明可以使用 SetOptions(button_color=sg.COLOR_SYSTEM_DEFAULT)
之类的东西来设置系统选项,但是我现在无法弄清楚要将设置应用于哪个对象。
有没有办法改变 wx.button
的 label
属性 的颜色?
我的代码超级简单:
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()
注释掉的行表示我到目前为止已经尝试过的一些事情。
编辑
下面的答案适用于 Python3,但使用 Python2,我得到以下结果:
使用按钮的 SetOwnForegroundColour
例如
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()
[MacOS卡特琳娜]
我完全是 wxPython 的菜鸟。我正在通过各种在线教程学习使用 wxPython,但我遇到了一些问题,我找不到答案。
因为我在 Mac 上使用 'Dark Mode',按钮的标签文本呈现为白色。这使它们难以阅读,我希望文本是黑色的。但是,我无法找到任何解释如何使用 wx.button 执行此操作的内容。我能找到的最接近的答案表明可以使用 SetOptions(button_color=sg.COLOR_SYSTEM_DEFAULT)
之类的东西来设置系统选项,但是我现在无法弄清楚要将设置应用于哪个对象。
有没有办法改变 wx.button
的 label
属性 的颜色?
我的代码超级简单:
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()
注释掉的行表示我到目前为止已经尝试过的一些事情。
编辑
下面的答案适用于 Python3,但使用 Python2,我得到以下结果:
使用按钮的 SetOwnForegroundColour
例如
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()