CustomTkinter中CTkLabel下划线功能异常问题求助
解决CustomTkinter中CTkLabel整段文本添加下划线的问题
你之前遇到的部分文本下划线问题,是因为CTkLabel的underline参数和原生Tkinter逻辑一致——它是用来指定单个字符的索引(比如underline=0会给第一个字符加下划线),而不是给整段文本添加下划线,所以设True或1只会给某个字符加线。
下面给两种能实现整段文本下划线的方法:
方法一:用CTkFont配置全局下划线
直接通过CTkFont对象定义带下划线的字体,再传给CTkLabel的font参数,整个文本都会应用下划线样式:
import customtkinter as ctk app = ctk.CTk() # 创建带下划线的自定义字体 underline_font = ctk.CTkFont(family="bahnschrift semilight", size=18, underline=True) instruction_head = ctk.CTkLabel(app, text="Instructions", font=underline_font) instruction_head.place(relx=.2, rely=.25, anchor=ctk.CENTER) app.mainloop()
方法二:使用富文本标签
CTkLabel支持简单的HTML富文本格式,直接用<u>标签包裹文本即可实现整段下划线:
import customtkinter as ctk app = ctk.CTk() instruction_head = ctk.CTkLabel(app, text="<u>Instructions</u>", font=("bahnschrift semilight", 18)) instruction_head.place(relx=.2, rely=.25, anchor=ctk.CENTER) app.mainloop()
内容的提问来源于stack exchange,提问作者Bruck
相关产品推荐
相关产品推荐

