0

Here is some simplified code that highlight my problem:

import time
import tkinter as tk
from tkinter import font


class Example(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        tic = time.perf_counter()

        for i in range(40):
            label = tk.Label(self, text = "testing this font", font = ("Courier", 18))
            label.pack()
            
        toc = time.perf_counter()
        print(f"{toc-tic:0.4f} seconds")

if __name__ == "__main__":
    root=tk.Tk()
    example = Example(root)
    example.pack(side="top", fill="both", expand=True)
    root.mainloop()

When I run this it takes about 2 seconds, however, if I remove "font=('Courier', 18)" from the tk.Label, then it takes about 0.03 seconds to load. Is there a reason why not defining a font would allow the frame to render so much faster?

Thank you in advance,

-John

user2714543
  • 699
  • 1
  • 6
  • 6
  • It depends on several factors. If you pick an 'easier' to render font like `Arial', it's faster. If you open a second window, it's faster as well. Never as fast as for the default settings, but that's probably because it's also the default for the underlying system involved in rendering the text. It's hard to say why exactly each option is a bit slower, but it's certainly a combination of many factors. (note that you see very different results on different OSes / window managers as well) – Grismar Jan 08 '22 at 01:35
  • Have you tried using a `Font` object instead of a tuple to define the font? – Bryan Oakley Jan 08 '22 at 02:19
  • @BryanOakley: Yeah, I tried a ```Font``` object but it took the same amount of render time as defining a Tuple. – user2714543 Jan 08 '22 at 02:43
  • I can't duplicate the problem. The code in this question comes up for me nearly instantly on my Mac and the output is `0.0160 seconds`. Maybe your system doesn't have a font by that exact name so it's having to do a little extra work to come up with a suitable substitute. If I use the font `(Bogus, 18)` it runs a little slower but still under 1/10th of a second. – Bryan Oakley Jan 08 '22 at 04:12

0 Answers0