wxPython - 更改按钮的前景色
在本文中,我们将学习如何更改按钮的前景色或按钮的字体颜色。为此,我们将使用与 wxPython 的 wx.Button 类关联的 SetForegroundColour()函数。
SetForegroundColour()函数只需将 wx.Colour 参数用于前景。
Syntax: wx.Button.SetForegroundColour(self, colour)
Parameters:
Parameter | Input Type | Description |
---|---|---|
colour | wx.Colour | colour for the foreground. |
代码示例:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
# create parent panel for button
self.pnl = wx.Panel(self)
# create button at point (20, 20)
self.btn = wx.Button(self.pnl, id = 1, label ="Button", pos =(20, 20),
size =(300, 40), name ="button")
# change foreground colour of button to blue
self.btn.SetForegroundColour((0, 0, 255, 255))
self.SetSize((350, 250))
self.SetTitle('wx.Button')
self.Centre()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出窗口: