📜  PYGLET - 访问标签文本字体名称属性(1)

📅  最后修改于: 2023-12-03 14:45:44.257000             🧑  作者: Mango

PYGLET - Accessing Label Text Font Name Property

Introduction

In PYGLET, a Label object is a graphical object that displays text. It is commonly used in GUI programming for displaying text on windows, buttons, and other UI elements. In this tutorial, we will learn how to access the font name property of a Label object.

Steps
  1. First, we need to import the pyglet library and create a window to display the label.
import pyglet

window = pyglet.window.Window()
  1. Next, we create a Label object and set its text and font name.
label = pyglet.text.Label('Hello World',
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
  1. To access the font name property of the Label object, we simply use the font_name attribute.
print(label.font_name)

This will output the font name 'Times New Roman'.

Full Code
import pyglet

window = pyglet.window.Window()

label = pyglet.text.Label('Hello World',
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')

print(label.font_name)

@window.event
def on_draw():
    window.clear()
    label.draw()

pyglet.app.run()
Conclusion

In this tutorial, we have learned how to access the font name property of a Label object in PYGLET. By setting the font name property, we can easily change the font type of the displayed text.