wxpython基本对象字体fonts用法示例

发布时间:2020-12-10编辑:脚本学堂
有关wxpython基本对象中字体对象的用法,通过wx.Font对象,可以创建不同种类的字体,wx.Font对象特定的参数选项,需要的朋友参考下。

通过 wx.Font 对象,可以创建不同种类的字体,它有作如下的构建器:
 

wx.Font(integer pointSize, wx.FontFamily family, integer style,
        integer weight, bool underline = false, string faceName = '',
        wx.FontEncoding encoding = wx.FONTENCODING_DEFAULT)

特定的参数可以有以下选项:
“family”:
 

wx.DEFAULT
wx.DECORATIVE
wx.ROMAN
wx.SWISS
wx.SCRIPT
wx.MODERN
“style”:
wx.NORMAL
wx.SLANT
wx.ITALIC

“weight”:
 

wx.NORMAL
wx.LIGHT
wx.BOLD

例子,fonts.py 程序展示了三种不同的字体.
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#fonts.py

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(325, 320))
       
        panel = wx.Panel(self, -1)
       
        text1 = "Now listen to me mamanMama mamanYou're taking a way my last chancenDon't take it away"
       
        text2 = '''You won't cry for my absence, I know -
        You forgot me long ago.
        Am I that unimportant...?
        Am I so insignificant...?
        Isn't something missing?
        Isn't someone missing me?'''
       
        text3 = '''But if I had one wish fulfilled tonight
        I'd ask for the sun to never rise
        If God passed a mic to me to speak
        I'd say stay in bed, world
        Sleepin peace'''
       
        font1 = wx.Font(10, wx.NORMAL, wx.ITALIC, wx.NORMAL)
        font2 = wx.Font(10, wx.ROMAN, wx.NORMAL, wx.NORMAL)
        font3 = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL)

        lyrics1 = wx.StaticText(panel, -1, text1, (30, 15), style=wx.ALIGN_CENTER)
        lyrics1.SetFont(font1)
        lyrics2 = wx.StaticText(panel, -1, text2, (30, 100), style=wx.ALIGN_CENTER)
        lyrics2.SetFont(font2)
        lyrics3 = wx.StaticText(panel, -1, text3, (30, 220), style=wx.ALIGN_CENTER)
        lyrics3.SetFont(font3)
        self.Center()
       
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'fonts.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
   
app = MyApp(0)
app.MainLoop()

如图:
wxpython基本对象字体fonts图:fonts.py