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
#11
If I may suggest something. Instead of using 1 and 2, use bool variable (and meaningful name :))

import pyglet
import time

use_colon = True
 
pyglet.font.add_file('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 use_colon
    label.text = time.strftime("%H:%M %P" if use_colon else "%H.%M %P")
    use_colon = not use_colon

pyglet.clock.schedule_interval(update_clock, 1)
 
@window.event
def on_draw():
    window.clear()
    label.draw()
 
pyglet.app.run()
however using global variables is bad, so a better solution is to define a class and work with it

import pyglet
import time

class DigitalClock:
    def __init__(self, window):
        self.use_colon = True
        self.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(self):
        self.label.text = time.strftime("%H:%M %P" if self.use_colon else "%H.%M %P")
        self.use_colon = not self.use_colon

def update_clock(dt, digital_clock):
    digital_clock.update()

 
pyglet.font.add_file('digital-7.ttf')
window = pyglet.window.Window()
window.set_fullscreen(True)


digital_clock = DigitalClock(window)
pyglet.clock.schedule_interval(update_clock, 1, digital_clock=digital_clock)
 
@window.event
def on_draw():
    window.clear()
    digital_clock.label.draw()
 
pyglet.app.run()
This is very basic example. the class can be customized at instantiation (font, size, etc.)

As to tkinter&pyglet code not working on Linux, but working on Windows - I suspect it's just one of those OS dependent quirks, not problem with your code per se...
If you insist on using tkinter, why not install the font, instead of trying to load it from file without installation? In this case no pyglet will be needed at all
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
#12
Wow and thanks again... Those last code a bit hard to understand how it works but I need more learning...
However, I did install the font and code with Tkinter can't see it. here what I did:
> cp Digital-7.ttf /home/pi/.local/fonts
> fc-cache -f -v

did I miss something?

Also, I use font list codes from the internet it show me list a lot but no Digital-7. here the code that I copied from the internet:

from tkinter import *
from tkinter import font

root = Tk()
root.title('Font Families')
fonts=list(font.families())
fonts.sort()

def populate(frame):
    '''Put in the fonts'''
    listnumber = 1
    for item in fonts:
        label = "listlabel" + str(listnumber)
        label = Label(frame,text=item,font=(item, 16)).pack()
        listnumber += 1

def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))

canvas = Canvas(root, borderwidth=0, background="#ffffff")
frame = Frame(canvas, background="#ffffff")
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

root.mainloop()

Font list snapshot:
[Image: view?usp=sharing]
Reply
#13
(Sep-16-2020, 12:43 PM)ATARI_LIVE Wrote: /home/pi/.local/fonts
not sure, but shouldn't it be in /home/pi/.local/share/fonts

https://askubuntu.com/q/3697

Also I see you do fc-cache, but it doesn't hurt to reboot :-)
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
#14
[Image: giphy.gif]

I put wrong path for the fonts. correct is /usr/share/fonts/truetype and do fc-cache and WORKED with my original code!
now it all resolved.

THANK YOU FOR HELPS it's lot!
Reply
#15
(Sep-16-2020, 03:32 PM)ATARI_LIVE Wrote: WORKED with my original code!
if the font is installed, no need of pyglet, so you can change your original code
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
#16
(Sep-16-2020, 03:45 PM)buran Wrote:
(Sep-16-2020, 03:32 PM)ATARI_LIVE Wrote: WORKED with my original code!
if the font is installed, no need of pyglet, so you can change your original code

Yup!
Reply
#17
I was looking for a solution to a similar font problem, but I didn't understand anything from what you wrote. Could you explain in human language how I can fix this problem with fonts? I sort of load my own salt life font, but it doesn't show up in the window. Am I making some kind of primitive mistake? I'm just here to find a solution, but as it turns out, I'm not very good at all these codes. If someone helped me, I would be very grateful. Thank you in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 322 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,788 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,695 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  can openpyxl read font colors mperemsky 3 1,747 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 972 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,180 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Folium: Conflict with Font Awesome Kit jgomes_eu 0 1,221 Apr-23-2022, 03:18 PM
Last Post: jgomes_eu
  Print text with big font and style tomtom 5 14,130 Mar-03-2022, 01:29 AM
Last Post: tomtom
  PIL Image im.show() no show! Pedroski55 6 4,936 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