这是一些突出我的问题的简化代码:
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()
当我运行它时,它大约需要 2 秒,但是,如果我从 tk.Label 中删除“font=('Courier', 18)”,则加载大约需要 0.03 秒。是否有理由不定义字体会使框架渲染得如此之快?
先感谢您,
-约翰