wxPython - 更改静态文本的字体
在本文中,我们将学习如何更改窗口上显示的静态文本的字体。首先,我们将创建一个 wxPython 的 wx.Font 类。之后我们将使用 wx.StaticText 类的 SetFont()函数。
SetFont()函数采用 sing wx.Font 类型参数。
Syntax: wx.StaticText.SetFont(self, font)
Parameters:
Parameter | Input Type | Description |
---|---|---|
font | wx.Font | Font for the static text. |
代码示例:
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)
self.pnl = wx.Panel(self)
# create wx.Font object
font = wx.Font(20, family = wx.FONTFAMILY_MODERN, style = 0, weight = 90,
underline = False, faceName ="", encoding = wx.FONTENCODING_DEFAULT)
self.st = wx.StaticText(self.pnl, id = 1, label ="This is the Label.", pos =(20, 20),
size = wx.DefaultSize, style = wx.ST_ELLIPSIZE_MIDDLE, name ="statictext")
# set font for the statictext
self.st.SetFont(font)
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()
输出窗口: