0

I am new to coding and I am creating things with Python that will allow me to explore the limitations of the programme and the packages and set me up to create more serious and useful programmes later. I have read loads of articles and the wiki guidance for fontTools but I cannot find the specific answer I am looking for and I imagine that what I am about to ask is impossible but I will go ahead anyway.

So yesterday's project was the make a Star Wars themed alarm clock with the Tkinter package which was simple enough and then I realised I could make multiple windows with Tkinter so I programmed a way for the first window to show "A long time ago in a galaxy far far away..." and then for a second window to show Star Wars and play the theme after a button was clicked:


import tkinter as tk
from tkinter import *
import threading
import winsound
from fontTools.ttLib import TTFont

font = TTFont("C:/Users/lugz2/PycharmProjects/StarWarsGUIChanger/Starjedi.ttf")


window = Tk()
window.config(bg="black")
starwars = tk.Label(
    text="A long time ago in a galaxy far far away...",
    fg="lightBlue",
    bg="black",
    height=800,
    width=400,
    font=(None, 20)
)
window.title("Star Wars")
window.geometry("800x400")
starwars.pack()


def play():
    winsound.PlaySound("swtheme.wav", winsound.SND_ALIAS)


def printtext():
    threadsound.start()
    print("STAR WARS")
    window2 = Tk()
    window2.config(bg="black")
    starwarsnew = tk.Label(
        window2,
        text="STAR WARS",
        fg="yellow",
        bg="black",
        height=800,
        width=400,
        font=(font, 80)
    )
    window2.title("STAR WARS")
    window2.geometry("800x400")
    starwarsnew.pack()


threadsound = threading.Thread(target=play)

button = Button(window, text="PLAY", fg="lightBlue", bg="black", width=10, command=printtext).place(x=350, y=300)


starwars.mainloop()

In the code above, you can see I added the "Starjedi.ttf" using fontTools which I wanted to then import into Tkinter to use for the "Star Wars" text in "window2" - I have tried LOADS of ways but nothing seems to work! I wondered if anyone could give me a heads up on whether it is possible and if so, how can I do that?

  • 2
    `fontlib` is for converting fonts between formats, and extracting information from them. It has absolutely nothing to do with actually displaying text in a font. All Tkinter needs to be able to use a font is for it to be installed in your operating system. – jasonharper Aug 18 '20 at 12:47
  • Oh really? I tried installing it and it didn't work that way either - perhaps I didn't type the write thing in the "font families" field! I will look into it! Thanks a lot! – Logan Lee Goodrum Aug 18 '20 at 13:43
  • Yeah, the name you use to refer to the font isn't necessarily the same as the filename. See https://stackoverflow.com/questions/39614027/list-available-font-families-in-tkinter for how to get a list of usable names. – jasonharper Aug 18 '20 at 14:14
  • Yeah, I had just put in the wrong name! It is working perfectly now, which means I can happily move on to the next project! Thanks Jason! – Logan Lee Goodrum Aug 18 '20 at 14:33

0 Answers0