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

时间:2016-08-27 08:21:26

标签: python python-3.x tkinter

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

enter image description here

1 个答案:

答案 0 :(得分:2)

无法更改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