Skip to main content
 首页 » 编程设计

python之在 tkinter 消息框中更改部分消息的颜色

2024年10月17日7wuhuacong

我有一个如下所示的 TKinter 消息框。我想将消息的部分更改为不同的颜色。例如,在下面的消息框中,我希望语言是 Blue。这可能吗?

请您参考如下方法:

无法更改 Tkinter Standard Dialogs 的此类选项.您需要创建自己的对话框。您还需要分隔文本部分。我试图在 OP 的图像中制作类似的东西已在上面发布:

from tkinter import * 
root = Tk() 
 
def choosefunc(option): 
    if option == "cancel": 
        print("Cancel choosen") 
    else: 
        print("OK choosen") 
 
def popupfunc(): 
 
    tl = Toplevel(root) 
    tl.title("Languages") 
 
    frame = Frame(tl) 
    frame.grid() 
 
    canvas = Canvas(frame, width=100, height=130) 
    canvas.grid(row=1, column=0) 
    imgvar = PhotoImage(file="pyrocket.png") 
    canvas.create_image(50,70, image=imgvar) 
    canvas.image = imgvar 
 
    msgbody1 = Label(frame, text="The", font=("Times New Roman", 20, "bold")) 
    msgbody1.grid(row=1, column=1, sticky=N) 
    lang = Label(frame, text="language(s)", font=("Times New Roman", 20, "bold"), fg='blue') 
    lang.grid(row=1, column=2, sticky=N) 
    msgbody2 = Label(frame, text="of this country is: Arabic", font=("Times New Roman", 20, "bold")) 
    msgbody2.grid(row=1, column=3, sticky=N) 
 
    cancelbttn = Button(frame, text="Cancel", command=lambda: choosefunc("cancel"), width=10) 
    cancelbttn.grid(row=2, column=3) 
 
    okbttn = Button(frame, text="OK", command=lambda: choosefunc("ok"), width=10) 
    okbttn.grid(row=2, column=4) 
 
label = Label(root, text="Click to proceed:") 
label.grid() 
 
button = Button(root, text="Click", command=popupfunc) 
button.grid() 

(图片网址:http://imgur.com/a/Nf75v)