wx.Font¶
A font is an object which determines the appearance of text.
Fonts are used for drawing text to a device context, and setting the appearance of a window’s text, see wx.DC.SetFont
and wx.Window.SetFont
.
The easiest way to create a custom font is to use wx.FontInfo object to specify the font attributes and then use Font.__init__
constructor. Alternatively, you could start with one of the pre-defined fonts or use wx.Window.GetFont
and modify the font, e.g. by increasing its size using MakeLarger
or changing its weight using MakeBold
.
This class uses reference counting and copy-on-write internally so that assignments between two instances of this class are very cheap. You can therefore use actual objects instead of pointers without efficiency problems. If an instance of this class is changed it will create its own data internally so that other instances, which previously shared the data using the reference counting, are not affected.
You can retrieve the current system font settings with wx.SystemSettings.
See also
Font Overview, wx.DC.SetFont
, wx.DC.DrawText
, wx.DC.GetTextExtent
, wx.FontDialog, wx.SystemSettings
Class Hierarchy¶
Methods Summary¶
Default constructor. |
|
Specify the name of a file containing a TrueType font to be made available to the current application. |
|
Returns a bold version of this font. |
|
Returns |
|
Returns a font with the same face/size as the given one but with normal weight and style and not underlined nor stricken through. |
|
Returns the current application’s default encoding. |
|
Returns the encoding of this font. |
|
Returns the face name associated with the font, or the empty string if there is no face information. |
|
Gets the font family if possible. |
|
Gets the point size as a floating number. |
|
Returns the font’s native handle. |
|
Returns a font with the same face/size as the given one but with normal weight and style and not underlined nor stricken through. |
|
Returns the platform-dependent string completely describing this font. |
|
Returns a user-friendly string for this font object. |
|
Gets the font weight as an integer value. |
|
Get the raw weight value corresponding to the given symbolic constant. |
|
Returns the font’s native handle. |
|
Gets the pixel size. |
|
Gets the point size as an integer number. |
|
Returns |
|
Gets the font style. |
|
Returns |
|
Gets the font weight. |
|
Returns |
|
Returns |
|
Returns an italic version of this font. |
|
Returns a larger version of this font. |
|
Changes this font to be bold. |
|
Changes this font to be italic. |
|
Changes this font to be larger. |
|
Changes this font to be smaller. |
|
Changes this font to be stricken-through. |
|
Changes this font to be underlined. |
|
This function takes the same parameters as the relative Font constructor and returns a new font object allocated on the heap. |
|
Returns the font’s native handle. |
|
Changes the size of this font. |
|
Returns a scaled version of this font. |
|
Sets the default font encoding. |
|
Sets the encoding for this font. |
|
Sets the facename for the font. |
|
Sets the font family. |
|
Sets the font size in points. |
|
Creates the font corresponding to the given native font description string which must have been previously returned by |
|
Creates the font corresponding to the given native font description string and returns |
|
Sets the font weight using an integer value. |
|
Sets the pixel size. |
|
Sets the font size in points to an integer value. |
|
Sets strike-through attribute of the font. |
|
Sets the font style. |
|
Sets the font size using a predefined symbolic size name. |
|
Sets the font size compared to the base font size. |
|
Sets underlining. |
|
Sets the font weight. |
|
Returns a smaller version of this font. |
|
Returns stricken-through version of this font. |
|
Returns underlined version of this font. |
|
For internal use only. |
|
Inequality operator. |
|
Equality operator. |
Properties Summary¶
See |
|
See |
|
See |
|
See |
|
Class API¶
- class wx.Font(GDIObject)¶
Possible constructors:
Font() -> None Font(font) -> None Font(fontInfo) -> None Font(pointSize, family, style, weight, underline=False, faceName='', encoding=FONTENCODING_DEFAULT) -> None Font(pixelSize, family, style, weight, underline=False, faceName='', encoding=FONTENCODING_DEFAULT) -> None Font(nativeInfoString) -> None Font(nativeInfo) -> None
A font is an object which determines the appearance of text.
Methods¶
- __init__(self, *args, **kw)¶
-
__init__ (self)
Default constructor.
- Return type:
None
__init__ (self, font)
Copy constructor, uses reference counting.
- Parameters:
font (wx.Font)
- Return type:
None
__init__ (self, fontInfo)
Creates a font object using the specified font description.
This is the preferred way to create font objects as using this constructor results in more readable code and it is also extensible, e.g. it could continue to be used if support for more font attributes is added in the future. For example, this constructor provides the only way of creating fonts with strike-through style.
Example of creating a font using this constructor:
# Create a font using wx.FontInfo font = wx.Font( wx.FontInfo(10).Bold().Underline() )
- Parameters:
fontInfo (wx.FontInfo)
- Return type:
None
Added in version 2.9.5.
__init__ (self, pointSize, family, style, weight, underline=False, faceName=’’, encoding=FONTENCODING_DEFAULT)
Creates a font object with the specified attributes and size in points.
Notice that the use of this constructor is often more verbose and less readable than using the constructor accepting a wx.FontInfo, e.g. the example in that constructor documentation would need to be written as:
# Create a font using the old-style constructor font = wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, True)
- Parameters:
pointSize (int) – Size in points. See
SetPointSize
for more info. Notice that, for historical reasons, the value 70 here is interpreted atDEFAULT
and results in creation of the font with the default size and not of a font with the size of 70pt. If you really need the latter, please use SetPointSize(70). Note that this constructor and the matching Create() method overload are the only places in wx.Font API handlingDEFAULT
specially: neitherSetPointSize
nor the constructor taking wx.FontInfo handle this value in this way.family (FontFamily) – The font family: a generic portable way of referring to fonts without specifying a facename. This parameter must be one of the wx.FontFamily enumeration values. If the faceName argument is provided, then it overrides the font family.
style (FontStyle) – One of
FONTSTYLE_NORMAL
,FONTSTYLE_SLANT
andFONTSTYLE_ITALIC
.weight (FontWeight) – Font weight, sometimes also referred to as font boldness. One of the wx.FontWeight enumeration values.
underline (bool) – The value can be
True
orFalse
. At present this has an effect on Windows and Motif 2.x only.faceName (string) – An optional string specifying the face name to be used. If it is an empty string, a default face name will be chosen based on the family.
encoding (FontEncoding) – An encoding which may be one of the enumeration values of wx.FontEncoding. If the specified encoding isn’t available, no font is created (see also Font Encodings).
- Return type:
None
Note
If the desired font does not exist, the closest match will be chosen. Under Windows, only scalable TrueType fonts are used.
__init__ (self, pixelSize, family, style, weight, underline=False, faceName=’’, encoding=FONTENCODING_DEFAULT)
Creates a font object with the specified attributes and size in pixels.
Notice that the use of this constructor is often more verbose and less readable than the use of constructor from wx.FontInfo, consider using that constructor instead.
- Parameters:
pixelSize (wx.Size) – Size in pixels. See
SetPixelSize
for more info.family (FontFamily) – The font family: a generic portable way of referring to fonts without specifying a facename. This parameter must be one of the wx.FontFamily enumeration values. If the faceName argument is provided, then it overrides the font family.
style (FontStyle) – One of
FONTSTYLE_NORMAL
,FONTSTYLE_SLANT
andFONTSTYLE_ITALIC
.weight (FontWeight) – Font weight, sometimes also referred to as font boldness. One of the wx.FontWeight enumeration values.
underline (bool) – The value can be
True
orFalse
. At present this has an effect on Windows and Motif 2.x only.faceName (string) – An optional string specifying the face name to be used. If it is an empty string, a default face name will be chosen based on the family.
encoding (FontEncoding) – An encoding which may be one of the enumeration values of wx.FontEncoding. If the specified encoding isn’t available, no font is created (see also Font Encodings).
- Return type:
None
Note
If the desired font does not exist, the closest match will be chosen. Under Windows, only scalable TrueType fonts are used.
__init__ (self, nativeInfoString)
Constructor from font description string.
This constructor uses
SetNativeFontInfo
to initialize the font. If fontdesc is invalid the font remains uninitialized, i.e. itsIsOk
method will returnFalse
.- Parameters:
nativeInfoString (string)
- Return type:
None
__init__ (self, nativeInfo)
Construct font from a native font info structure.
- Parameters:
nativeInfo (wx.NativeFontInfo)
- Return type:
None
- static AddPrivateFont(filename)¶
Specify the name of a file containing a TrueType font to be made available to the current application.
This method can be used to allow this application to use the font from the given file even if it is not globally installed on the system.
Under macOS this method actually doesn’t do anything other than check for the existence of the file in the “Fonts” subdirectory of the application bundle “Resources” directory. You are responsible for actually making the font file available in this directory and setting
ATSApplicationFontsPath
toFonts
value in yourInfo.plist
file. See alsowx.StandardPaths.GetResourcesDir
.Under MSW this method must be called before any wx.GraphicsContext objects have been created, otherwise the private font won’t be usable from them.
Under Unix this method requires Pango 1.38 or later and will return
False
and log an error message explaining the problem if this requirement is not satisfied either at compile- or run-time.Currently this method is implemented for all major platforms (subject to having Pango 1.38 or later when running configure under Unix) and
USE_PRIVATE_FONTS
is always set to 0 under the other platforms, making this function unavailable at compile-time.- Parameters:
filename (string)
- Return type:
bool
- Returns:
True
if the font was added and can now be used.
Added in version 4.1/wxWidgets-3.1.1.
- Bold(self)¶
Returns a bold version of this font.
- Return type:
Added in version 2.9.1.
See also
- static CanUsePrivateFont()¶
Returns
True
if this build of wxPython supports usingAddPrivateFont
.- Return type:
bool
- GetBaseFont(self)¶
Returns a font with the same face/size as the given one but with normal weight and style and not underlined nor stricken through.
- Return type:
Added in version 4.1/wxWidgets-3.1.0.
- static GetDefaultEncoding()¶
Returns the current application’s default encoding.
- Return type:
See also
- GetEncoding(self)¶
Returns the encoding of this font.
Note that under wxGTK the returned value is always
FONTENCODING_UTF8
.- Return type:
See also
- GetFaceName(self)¶
Returns the face name associated with the font, or the empty string if there is no face information.
- Return type:
str
See also
- GetFamily(self)¶
Gets the font family if possible.
As described in wx.FontFamily docs the returned value acts as a rough, basic classification of the main font properties (look, spacing).
If the current font face name is not recognized by wx.Font or by the underlying system,
FONTFAMILY_DEFAULT
is returned.Note that currently this function is not very precise and so not particularly useful. Font families mostly make sense only for font creation, see
SetFamily
.- Return type:
See also
- GetFractionalPointSize(self)¶
Gets the point size as a floating number.
- Return type:
float
Added in version 4.1/wxWidgets-3.1.2.
See also
SetPointSize(float)
- GetHFONT(self)¶
Returns the font’s native handle.
- Return type:
Any
- GetNativeFontInfo(self)¶
Returns a font with the same face/size as the given one but with normal weight and style and not underlined nor stricken through.
- Return type:
Added in version 4.1/wxWidgets-3.1.0.
- GetNativeFontInfoDesc(self)¶
Returns the platform-dependent string completely describing this font.
Returned string is always non-empty unless the font is invalid (in which case an assert is triggered).
Note that the returned string is not meant to be shown or edited by the user: a typical use of this function is for serializing in string-form a wx.Font object.
- Return type:
str
See also
- GetNativeFontInfoUserDesc(self)¶
Returns a user-friendly string for this font object.
Returned string is always non-empty unless the font is invalid (in which case an assert is triggered).
The string does not encode all wx.Font infos under all platforms; e.g. under wxMSW the font family is not present in the returned string.
Some examples of the formats of returned strings (which are platform-dependent) are in
SetNativeFontInfoUserDesc
.- Return type:
str
See also
- GetNumericWeight(self)¶
Gets the font weight as an integer value.
See wx.FontWeight for a list of valid weight identifiers and their corresponding integer value.
- Return type:
int
Added in version 4.1/wxWidgets-3.1.2.
See also
See also
- static GetNumericWeightOf(weight)¶
Get the raw weight value corresponding to the given symbolic constant.
For compatibility, this function handles the values
NORMAL
,LIGHT
andBOLD
, that have values 90, 91 and 92, specially and converts them to the correspondingFONTWEIGHT_XXX
weight value.- Parameters:
weight (FontWeight) – A valid element of FontWeight enum, i.e. this argument can’t have value
FONTWEIGHT_INVALID
.- Return type:
int
- Returns:
Numeric weight, between 1 and 1000.
Added in version 4.1/wxWidgets-3.1.2.
- GetPangoFontDescription(self)¶
Returns the font’s native handle.
- Return type:
Any
- GetPixelSize(self)¶
Gets the pixel size.
Note that under wxMSW if you passed to
SetPixelSize
(or to the constructor) a wx.Size object with a null width value, you’ll get a null width in the returned object.- Return type:
See also
- GetPointSize(self)¶
Gets the point size as an integer number.
This function is kept for compatibility reasons. New code should use
GetFractionalPointSize
and support fractional point sizes.- Return type:
int
See also
See also
- GetStrikethrough(self)¶
Returns
True
if the font is stricken-through,False
otherwise.- Return type:
bool
Added in version 2.9.4.
See also
- GetStyle(self)¶
Gets the font style.
See wx.FontStyle for a list of valid styles.
- Return type:
See also
- GetUnderlined(self)¶
Returns
True
if the font is underlined,False
otherwise.- Return type:
bool
See also
- GetWeight(self)¶
Gets the font weight.
See wx.FontWeight for a list of valid weight identifiers.
- Return type:
See also
- IsFixedWidth(self)¶
Returns
True
if the font is a fixed width (or monospaced) font,False
if it is a proportional one or font is invalid.Note that this function under some platforms is different from just testing for the font family being equal to
FONTFAMILY_TELETYPE
because native platform-specific functions are used for the check (resulting in a more accurate return value).- Return type:
bool
- IsOk(self)¶
Returns
True
if this object is a valid font,False
otherwise.- Return type:
bool
- Italic(self)¶
Returns an italic version of this font.
- Return type:
Added in version 2.9.1.
See also
- Larger(self)¶
Returns a larger version of this font.
The font size is multiplied by
1.2
, the factor of1.2
being inspired by theW3C
CSS specification.- Return type:
Added in version 2.9.1.
See also
MakeLarger
,Smaller
,Scaled
- MakeBold(self)¶
Changes this font to be bold.
- Return type:
Added in version 2.9.1.
See also
- MakeItalic(self)¶
Changes this font to be italic.
- Return type:
Added in version 2.9.1.
See also
- MakeLarger(self)¶
Changes this font to be larger.
The font size is multiplied by
1.2
, the factor of1.2
being inspired by theW3C
CSS specification.- Return type:
Added in version 2.9.1.
See also
Larger
,MakeSmaller
,Scale
- MakeSmaller(self)¶
Changes this font to be smaller.
The font size is divided by
1.2
, the factor of1.2
being inspired by theW3C
CSS specification.- Return type:
Added in version 2.9.1.
See also
Smaller
,MakeLarger
,Scale
- MakeStrikethrough(self)¶
Changes this font to be stricken-through.
Currently stricken-through fonts are only supported in wxMSW, wxGTK, and wxOSX.
- Return type:
Added in version 2.9.4.
See also
- MakeUnderlined(self)¶
Changes this font to be underlined.
- Return type:
Added in version 2.9.2.
See also
- static New(*args, **kw)¶
This function takes the same parameters as the relative Font constructor and returns a new font object allocated on the heap.
Their use is discouraged, use wx.Font constructor from wx.FontInfo instead.
New (pointSize, family, style, weight, underline=False, faceName=’’, encoding=FONTENCODING_DEFAULT)
- Parameters:
pointSize (int)
family (FontFamily)
style (FontStyle)
weight (FontWeight)
underline (bool)
faceName (string)
encoding (FontEncoding)
- Return type:
New (pointSize, family, flags=FONTFLAG_DEFAULT, faceName=’’, encoding=FONTENCODING_DEFAULT)
- Parameters:
pointSize (int)
family (FontFamily)
flags (int)
faceName (string)
encoding (FontEncoding)
- Return type:
New (pixelSize, family, style, weight, underline=False, faceName=’’, encoding=FONTENCODING_DEFAULT)
- Parameters:
pixelSize (wx.Size)
family (FontFamily)
style (FontStyle)
weight (FontWeight)
underline (bool)
faceName (string)
encoding (FontEncoding)
- Return type:
New (pixelSize, family, flags=FONTFLAG_DEFAULT, faceName=’’, encoding=FONTENCODING_DEFAULT)
- Parameters:
pixelSize (wx.Size)
family (FontFamily)
flags (int)
faceName (string)
encoding (FontEncoding)
- Return type:
New (nativeInfo)
- Parameters:
nativeInfo (wx.NativeFontInfo)
- Return type:
New (nativeInfoString)
- Parameters:
nativeInfoString (string)
- Return type:
- OSXGetCGFont(self)¶
Returns the font’s native handle.
- Return type:
Any
- Scale(self, x)¶
Changes the size of this font.
The font size is multiplied by the given factor (which may be less than 1 to create a smaller version of the font).
- Parameters:
x (float)
- Return type:
Added in version 2.9.1.
See also
- Scaled(self, x)¶
Returns a scaled version of this font.
The font size is multiplied by the given factor (which may be less than 1 to create a smaller version of the font).
- Parameters:
x (float)
- Return type:
Added in version 2.9.1.
- static SetDefaultEncoding(encoding)¶
Sets the default font encoding.
- Parameters:
encoding (FontEncoding)
- Return type:
None
See also
- SetEncoding(self, encoding)¶
Sets the encoding for this font.
Note that under wxGTK this function has no effect (because the underlying Pango library always uses
FONTENCODING_UTF8
).- Parameters:
encoding (FontEncoding)
- Return type:
None
See also
- SetFaceName(self, faceName)¶
Sets the facename for the font.
- Parameters:
faceName (string) – A valid facename, which should be on the end-user’s system.
- Return type:
bool
- Returns:
True
if the given face name exists; if the face name doesn’t exist in the user’s system then the font is invalidated (so thatIsOk
will returnFalse
) andFalse
is returned.
Note
To avoid portability problems, don’t rely on a specific face, but specify the font family instead (see wx.FontFamily and
SetFamily
).See also
- SetFamily(self, family)¶
Sets the font family.
As described in wx.FontFamily docs the given family value acts as a rough, basic indication of the main font properties (look, spacing).
Note that changing the font family results in changing the font face name.
- Parameters:
family (FontFamily) – One of the wx.FontFamily values.
- Return type:
None
See also
- SetFractionalPointSize(self, pointSize)¶
Sets the font size in points.
The point size is defined as 1/72 of the Anglo-Saxon inch (25.4 mm): it is approximately 0.0139 inch or 352.8 um.
- Parameters:
pointSize (float) – Size in points. This can also be a fractional point size like 11.5.
- Return type:
None
Added in version 4.1/wxWidgets-3.1.2.
See also
- SetNativeFontInfo(self, *args, **kw)¶
-
SetNativeFontInfo (self, info)
Creates the font corresponding to the given native font description string which must have been previously returned by
GetNativeFontInfoDesc
.If the string is invalid, font is unchanged. This function is typically used for de-serializing a wx.Font object previously saved in a string-form.
- Parameters:
info (string)
- Return type:
bool
- Returns:
True
if the creation was successful.
See also
SetNativeFontInfo (self, info)
Sets the encoding for this font.
Note that under wxGTK this function has no effect (because the underlying Pango library always uses
FONTENCODING_UTF8
).- Parameters:
info (wx.NativeFontInfo)
- Return type:
None
See also
- SetNativeFontInfoUserDesc(self, info)¶
Creates the font corresponding to the given native font description string and returns
True
if the creation was successful.Unlike
SetNativeFontInfo
, this function accepts strings which are user-friendly. Examples of accepted string formats are:platform
generic syntax
example
wxGTK2
[underlined] [strikethrough] [FACE-NAME] [bold] [oblique|italic] [POINTSIZE]
Monospace bold 10
wxMSW
[light|bold] [italic] [FACE-NAME] [POINTSIZE] [ENCODING]
Tahoma 10
WINDOWS-1252
For more detailed information about the allowed syntaxes you can look at the documentation of the native API used for font-rendering (e.g.
pango_font_description_from_string
under GTK, although notice that it doesn’t support the “underlined” and “strikethrough” attributes and so those are handled by wxWidgets itself).Note that unlike
SetNativeFontInfo
, this function doesn’t always restore all attributes of the wx.Font object under all platforms; e.g. on wxMSW the font family is not restored (because GetNativeFontInfoUserDesc doesn’t return it on wxMSW). If you want to serialize/deserialize a font in string form, you should useGetNativeFontInfoDesc
andSetNativeFontInfo
instead.- Parameters:
info (string)
- Return type:
bool
See also
Todo
add an example for Mac
- SetNumericWeight(self, weight)¶
Sets the font weight using an integer value.
See wx.FontWeight for a list of valid weight identifiers and their corresponding integer value.
- Parameters:
weight (int) – An integer value int the range
1-1000
.- Return type:
None
See also
- SetPixelSize(self, pixelSize)¶
Sets the pixel size.
The height parameter of pixelSize must be positive while the width parameter may also be zero (to indicate that you’re not interested in the width of the characters: a suitable width will be chosen for best rendering).
This feature (specifying the font pixel size) is directly supported only under wxMSW and wxGTK currently; under other platforms a font with the closest size to the given one is found using binary search (this maybe slower).
- Parameters:
pixelSize (wx.Size)
- Return type:
None
See also
- SetPointSize(self, pointSize)¶
Sets the font size in points to an integer value.
This is a legacy version of the function only supporting integer point sizes. It can still be used, but to avoid unnecessarily restricting the font size in points to integer values, consider using the new (added in wxWidgets 3.1.2)
SetFractionalPointSize
function instead.- Parameters:
pointSize (int)
- Return type:
None
- SetStrikethrough(self, strikethrough)¶
Sets strike-through attribute of the font.
Currently stricken-through fonts are only supported in wxMSW, wxGTK, and wxOSX.
- Parameters:
strikethrough (bool) –
True
to add strike-through style,False
to remove it.- Return type:
None
Added in version 2.9.4.
See also
- SetStyle(self, style)¶
Sets the font style.
- Parameters:
style (FontStyle) – One of the wx.FontStyle enumeration values.
- Return type:
None
See also
- SetSymbolicSize(self, size)¶
Sets the font size using a predefined symbolic size name.
This function allows changing font size to be (very) large or small compared to the standard font size.
- Parameters:
size (FontSymbolicSize)
- Return type:
None
Added in version 2.9.2.
See also
- SetSymbolicSizeRelativeTo(self, size, base)¶
Sets the font size compared to the base font size.
This is the same as
SetSymbolicSize
except that it uses the given font size as the normal font size instead of the standard font size.- Parameters:
size (FontSymbolicSize)
base (int)
- Return type:
None
Added in version 2.9.2.
- SetUnderlined(self, underlined)¶
Sets underlining.
- Parameters:
underlined (bool) –
True
to underline,False
otherwise.- Return type:
None
See also
- SetWeight(self, weight)¶
Sets the font weight.
- Parameters:
weight (FontWeight) – One of the wx.FontWeight values.
- Return type:
None
See also
- Smaller(self)¶
Returns a smaller version of this font.
The font size is divided by
1.2
, the factor of1.2
being inspired by theW3C
CSS specification.- Return type:
Added in version 2.9.1.
See also
MakeSmaller
,Larger
,Scaled
- Strikethrough(self)¶
Returns stricken-through version of this font.
Currently stricken-through fonts are only supported in wxMSW, wxGTK, and wxOSX.
- Return type:
Added in version 2.9.4.
See also
- Underlined(self)¶
Returns underlined version of this font.
- Return type:
Added in version 2.9.2.
See also
- __bool__(self)¶
- Return type:
bool
- __nonzero__(self)¶
- Return type:
bool
- _copyFrom(self, other)¶
For internal use only.
- Return type:
None
- __ne__(self, font)¶
Inequality operator.
See reference-counted object comparison for more info.
- Parameters:
font (wx.Font)
- Return type:
bool
- __eq__(self, font)¶
Equality operator.
See reference-counted object comparison for more info.
- Parameters:
font (wx.Font)
- Return type:
bool
Properties¶
- Encoding¶
See
GetEncoding
andSetEncoding
- FaceName¶
See
GetFaceName
andSetFaceName
- NativeFontInfoDesc¶
- NativeFontInfoUserDesc¶
- PixelSize¶
See
GetPixelSize
andSetPixelSize
- PointSize¶
See
GetPointSize
andSetPointSize