Python Forum

Full Version: Load external font and does not show font in the window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Confused the Coding with the font... Huh Doh

I use to download the font at:
https://www.1001freefonts.com/digital-play-st.font

Here code what I am trying:
from tkinter import *
import pyglet

win = Tk()

win.geometry("500x200")
win.title("FONT TEST")

pyglet.font.add_file('/home/pi/.fonts/digital.ttf')

set_font=pyglet.font.load('Digital Play St')

print(set_font)

lab2=Label(win, text="0 1 2 3 4 5 6 7 8 9", font=('set_font',19))
lab2.place(x=10,y=10)

win.mainloop()


I got the result is NOT what I wanted with the font:
[Image: view?usp=sharing]

Supposed be like this:
[Image: ed4c85679cf0100d7b1a62cea923978b.png]

Thanks for help!!! Type

I forgot to add, I renamed Digital Play St.ttf to digital.ttf
I think you need
lab2=Label(win, text="0 1 2 3 4 5 6 7 8 9", font=('Digital Play St',19))
https://stackoverflow.com/a/61353191/4046632
Thanks for answered, I changed it and same the result. ummm....

Tried different 5 ttf files and make sure ch-mod on those to accessible still no luck.
must be something missing in the coding. Tried google no helps.
I am at home now and I can test. I am not able to get the expect result on Linux, in Tkinter
with just pyglet it works
import pyglet
pyglet.font.add_file('Digital Play St.ttf')
window = pyglet.window.Window()



label = pyglet.text.Label('0 1 2 3 4 5 6 7 8 9',
                          font_name='Digital Play St',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')

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

pyglet.app.run()
will look further into it
Thank you! it's works... but strange.... My code not work on the raspberry Pi 4B but WORKED on python for window.
(Sep-15-2020, 03:12 PM)ATARI_LIVE Wrote: [ -> ]My code not work on the raspberry Pi 4B but WORKED on python for window.
I started to suspect that pyglet + tkinter works only on windows - it looks all who claim it works on SO are on windows. by the way, here in the comments it claim file name and font name should be the same in order pyglet to work:https://stackoverflow.com/questions/38815758/adding-downloaded-fonts-to-tkinter

So, does pyglet, without tkinter work for you?
(Sep-15-2020, 03:18 PM)buran Wrote: [ -> ]So, does pyglet, without tkinter work for you?

Yes, I guess I am go ahead use pyglet then. I am trying to make Alarm Clock with raspberry pi.

THANK YOU FOR YOUR HELP!
Ummm... I was trying for clock here the code:
import pyglet
from pyglet import clock
from time import strftime

pyglet.font.add_file('/home/pi/.fonts/digital-7.ttf')
window = pyglet.window.Window()
window.set_fullscreen(False)

while True:
    time_now=strftime('%H:%M:%S %P')
    label = pyglet.text.Label(time_now,
                            font_name='Digital-7',
                            font_size=300,
                            x=window.width//2, y=window.height//2,
                            anchor_x='center', anchor_y='center')

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

pyglet.app.run()
Can do that?
you should not use while loop. you need to make a function and call it periodically
read more about keeping track of time

import pyglet
import time
 
pyglet.font.add_file('digital-7.ttf')
window = pyglet.window.Window()
window.set_fullscreen(False)
label = pyglet.text.Label('',
                    font_name='Digital-7',
                    font_size=50,
                    x=window.width//2, y=window.height//2,
                    anchor_x='center', anchor_y='center')

def update_clock(dt):
    label.text = time.strftime('%H:%M:%S %P')

pyglet.clock.schedule_interval(update_clock, 0.1) # this will update 10 times a second, you may change that


@window.event
def on_draw():
    window.clear()
    label.draw()
 
pyglet.app.run()
Ah it's success! Dance Thanks!
Code here:
import pyglet
import time

i = 2
time_print="%H:%M %P"

pyglet.font.add_file('/home/pi/.fonts/Digital-7.ttf')
window = pyglet.window.Window()
window.set_fullscreen(True)
label = pyglet.text.Label('',
                    font_name='Digital-7',
                    font_size=400,
                    x=window.width//2, y=window.height//2,
                    anchor_x='center', anchor_y='center')

def update_clock(dt):

    global i
    global time_print
    label.text = time.strftime(time_print)
    if i == 2:
        time_print="%H,%M %P"
        i=1
    elif i == 1:
        time_print="%H:%M %P"
        i = 2
    return


pyglet.clock.schedule_interval(update_clock, 1)

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

pyglet.app.run()
Also I edited the Digital-7.ttf due to keep center and width to stay with : and ; and 1. (; is blank better than the space).

But the pyglet seem more complex than the tkinter. I like tkinter than pyglet more headache for me. I am gonna keep to trying with tkinter because wanted use button and many sizes of fonts each other texts on same the clock screen. Type

Buran or anyone, if you have time, would you find what's wrong with my original coding with the Tkinter as unable to load the font?

My original code does not load the font from the raspberry pi 4B but WORKS on my Python for windows (include exactly copied all modules from raspberry pi to windows)


Thank You So Much a Million!
Pages: 1 2