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


Messages In This Thread
RE: Load external font and does not show font in the window - by buran - Sep-16-2020, 11:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 375 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 2,017 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,779 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  can openpyxl read font colors mperemsky 3 1,858 May-09-2023, 11:18 AM
Last Post: MindKeeper
  Comparing two columns with same value but different font format doug2019 1 769 Jan-08-2023, 02:58 PM
Last Post: Larz60+
  PIL Image im.show() no show! Pedroski55 2 1,032 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,228 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Folium: Conflict with Font Awesome Kit jgomes_eu 0 1,263 Apr-23-2022, 03:18 PM
Last Post: jgomes_eu
  Print text with big font and style tomtom 5 14,462 Mar-03-2022, 01:29 AM
Last Post: tomtom
  PIL Image im.show() no show! Pedroski55 6 5,184 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