按钮文本的 Tkinter 格式部分

问题描述 投票:0回答:0

我有一个 customtkinter 按钮,看起来像这样。

有什么方法可以格式化这个按钮文本的一部分吗?例如,我希望第一行(“Decline Dumbell Press”)为粗体,第二行的文本(“Sets: 3, Reps: 8”)为斜体。这可以在同一个按钮中完成吗?

这是我的代码的截图。

exerciseSliced = "Dumbell Press"
sets="3"
reps="8"
button_text = exerciseSliced + "\n" + "Sets: " + sets + " Reps: " + reps
exerciseButton = customtkinter.CTkButton(master=tabName, text=button_text, width=525, height=70, corner_radius=10, fg_color=t.frameColours, command=lambda text=exercise: workoutDescription(text), font=t.textFont2)
exerciseButton.grid(pady=0, padx=0)

这是我尝试过的一些东西。

1。 ANSI 转义序列

variable = "The bold text is",'\033[1m' + 'Python' + '\033[0m'
b1 = tk.Button(f1, text=variable, width=100, height=5)
b1.pack()

这提供了以下输出

2。创建了一个颜色类

class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'
   
variable = "The output is:" + color.BOLD + 'Python Programming !' + color.BLUE
b1 = tk.Button(f1, text=variable, width=100, height=5)
b1.pack()

3。我也试过 HTML 标签,但没有用。

string tkinter button string-formatting customtkinter
© www.soinside.com 2019 - 2024. All rights reserved.