python label字体_使用外部字体作为标签的Python Pyglet

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 tried searching online for an hour now and nothing seems to be working. Added some code for reference:

font.add_file('ZukaDoodle.ttf')

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

PlayLabel = pyglet.text.Label('Go', font_name='ZukaDoodle', font_size=100, x=window.width // 2,

y=window.height - 450, anchor_x='center', anchor_y='center',

batch=buttons_batch,

color=(0, 0, 0, 1000), width=250, height=130)

解决方案

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

class main(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 = 1

def on_draw(self):

self.render()

def on_close(self):

self.alive = 0

def on_mouse_motion(self, x, y, dx, dy):

self.mouse_x = x

def on_key_release(self, symbol, modifiers):

try:

del self.keys[symbol]

except:

pass

def on_key_press(self, symbol, modifiers):

if symbol == key.ESCAPE: # [ESC]

self.alive = 0

self.keys[symbol] = True

def render(self):

self.clear()

self.PlayLabel.draw()

## Add stuff you want to render here.

## Preferably in the form of a batch.

self.flip()

def run(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值