Skip to content Skip to sidebar Skip to footer

Python Pyglet Using External Fonts For Labels

I'm working on a project at the moment and I have been trying to change the fonts of the labels in the pyglet library to fonts that I found online but I couldn't get it to work. I

Solution 1:

So the error is pretty simple. The loaded font-name is not ZukaDoodle, it's Zuka Doodle with a space. Here's a working executable sample:

from pyglet import *
from pyglet.gl import *

font.add_file('ZukaDoodle.ttf')
ZukaDoodle = font.load('ZukaDoodle.ttf', 16)

key = pyglet.window.key

classmain(pyglet.window.Window):
    def__init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)
        self.x, self.y = 0, 0

        self.keys = {}

        self.mouse_x = 0
        self.mouse_y = 0

        self.PlayLabel = pyglet.text.Label('Go', font_name='Zuka Doodle', font_size=100,
                                            x=self.width // 2,
                                            y=self.height - 450,
                                            anchor_x='center', anchor_y='center', 
                                            color=(255, 0, 0, 255),
                                            width=250, height=130)

        self.alive = 1defon_draw(self):
        self.render()

    defon_close(self):
        self.alive = 0defon_mouse_motion(self, x, y, dx, dy):
        self.mouse_x = x

    defon_key_release(self, symbol, modifiers):
        try:
            del self.keys[symbol]
        except:
            passdefon_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

        self.keys[symbol] = Truedefrender(self):
        self.clear()

        self.PlayLabel.draw()

        ## Add stuff you want to render here.## Preferably in the form of a batch.

        self.flip()

    defrun(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------# This is what replaces pyglet.app.run()# but is required for the GUI to not freeze#
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()

The key difference here is font_name='Zuka Doodle'. Also, the alpha channel usually doesn't need to be higher than 255 since that's the max value of a colored byte, so unless you're using 16-bit color representation per channel, 255 will be the maximum value.

Post a Comment for "Python Pyglet Using External Fonts For Labels"