Python Forum
Load external font and does not show font in the window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load external font and does not show font in the window
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you! it's works... but strange.... My code not work on the raspberry Pi 4B but WORKED on python for window.
Reply
#6
(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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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!
Reply
#8
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?
Reply
#9
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 319 Jan-14-2024, 03:56 AM
Last Post: 1418
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,777 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,686 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  can openpyxl read font colors mperemsky 3 1,740 May-09-2023, 11:18 AM
Last Post: MindKeeper
  Comparing two columns with same value but different font format doug2019 1 722 Jan-08-2023, 02:58 PM
Last Post: Larz60+
  PIL Image im.show() no show! Pedroski55 2 967 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,177 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Folium: Conflict with Font Awesome Kit jgomes_eu 0 1,219 Apr-23-2022, 03:18 PM
Last Post: jgomes_eu
  Print text with big font and style tomtom 5 14,104 Mar-03-2022, 01:29 AM
Last Post: tomtom
  PIL Image im.show() no show! Pedroski55 6 4,927 Feb-08-2022, 06:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020