Tkinter 异常:`_tkinter.TclError: unknown option "-font"` - ttkbootstrap 字体配置问题
根据错误信息显示,_tkinter.TclError: unknown option '-font',这意味着在 st.configure(font=('G语.ttf', 12)) 中尝试配置字体时出现了错误。
这是由于 ttkbootstrap 库的 ScrolledText 小部件不支持直接通过 configure 方法来配置字体。在这种情况下,您可以使用 configure_widget 方法来应用字体样式。请按照以下修改后的代码示例进行尝试:
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledText
import time
import os
root = ttk.Window()
style = ttk.Style("darkly")
def root_set():
root.geometry('500x400')
root.title('G语-翻译器')
def main(text):
global st
st.insert(END, f'{text}')
root.update()
frame.update()
st.configure_widget('text', font=('G语.ttf', 12)) # 应用G语.ttf字体
ent = ttk.Entry(width=40)
ent.place(x=20, y=50)
but = ttk.Button(text='翻译', width=39, command=lambda: main(ent.get()))
but.place(x=20, y=100)
frame = ttk.Frame(width=450, height=250)
frame.place(x=20, y=150)
st = ScrolledText(frame, padding=5, height=8, autohide=True)
st.place(x=0, y=0)
root_set()
root.mainloop()
请确保将 'G语.ttf' 替换为确切的字体文件路径,并确保该字体文件存在于指定位置。这样,main 函数中的文本插入将使用指定的字体进行显示。如果字体路径不正确或字体文件不存在,可能会导致错误。请相应地调整字体路径和文件名。
如果问题仍然存在,请提供完整的错误信息,以便我能够更好地帮助您解决问题。
原文地址: https://www.cveoy.top/t/topic/bAbu 著作权归作者所有。请勿转载和采集!