Class GLabel

This class is exactly like a GRectangle, except that it has the possibility of containing some text.

The attribute text defines the text content of this label. Uses of the escape character '\n' will result in a label that spans multiple lines. As with any GRectangle, the background color of this rectangle is fillcolor, while linecolor is the color of the text.

The text itself is aligned within this rectangle according to the attributes halign and valign. See the documentation of these attributes for how alignment works. There are also attributes to change the point size, font style, and font name of the text. The width and height of this label will grow to ensure that the text will fit in the rectangle, no matter the font or point size.

To change the font, you need a .ttf (TrueType Font) file in the Fonts folder; refer to the font by filename, including the .ttf. If you give no name, it will use the default Kivy font. The bold attribute only works for the default Kivy font; for other fonts you will need the .ttf file for the bold version of that font. See the provided ComicSans.ttf and ComicSansBold.ttf for an example.

Constructor

class game2d.grectangle.GLabel(**keywords)

A class representing an (uneditable) text label

This object is exactly like a GRectangle, except that it has the possibility of containing some text.

The attribute text defines the text content of this label. Uses of the escape character ‘n’ will result in a label that spans multiple lines. As with any GRectangle, the background color of this rectangle is fillcolor, while linecolor is the color of the text.

The text itself is aligned within this rectangle according to the attributes halign and valign. See the documentation of these attributes for how alignment works. There are also attributes to change the point size, font style, and font name of the text. The width and height of this label will grow to ensure that the text will fit in the rectangle, no matter the font or point size.

To change the font, you need a .ttf (TrueType Font) file in the Fonts folder; refer to the font by filename, including the .ttf. If you give no name, it will use the default Kivy font. The bold attribute only works for the default Kivy font; for other fonts you will need the .ttf file for the bold version of that font. See the provided ComicSans.ttf and ComicSansBold.ttf for an example.

Attributes

This class has all of the attributes of GRectangle. In addition, it has the following new attributes.

GLabel.text

The text for this label.

The text in the label is displayed as a single line, or broken up into multiple lines in the presence of the escape character ‘n’. The width and height of this label will grow to ensure that the text will fit in the rectangle.

Invariant: Must be a string

GLabel.font_size

The size of the text font in points.

Invariant: Must be a positive number (int or float)

GLabel.font_name

The file name for the .ttf file to use as a font

Invariant: Must be a string referring to a .ttf file in folder Fonts

GLabel.bold

A boolean indicating whether or not the text should be bold.

This value only works on the default Kivy font. It does not work on custom .ttf files. In that case, you need the bold version of the .ttf file. See ComicSans.ttf and ComicSansBold.ttf for an example.

Invariant: Must be a boolean

GLabel.halign

The horizontal alignment for this label. The text is horizontally anchored inside of the label rectangle at either the left, the right or the center. This means that as the size of the label increases, the text will still stay rooted at that anchor. By default, the text is centered.

This attribute has no effect unless the label rectangle is larger than the text it contains. This attribute only applies to the position of the text inside of the box. It cannot be used to center the text on screen.

Invariant: Must be one of ‘left’, ‘right’, or ‘center’

GLabel.valign

Vertical alignment for this label.

The text is vertically anchored inside of the label rectangle at either the top, the bottom or the middle. This means that as the size of the label increases, the text will still stay rooted at that anchor. By default, the text is in the middle.

This attribute has no effect unless the label rectangle is larger than the text it contains. This attribute only applies to the position of the text inside of the box. It cannot be used to center the text on screen.

Invariant: Must be one of ‘top’, ‘bottom’, or ‘middle’

Methods

This class does not have any methods beyond those defined in GObject.

Return to top level