在python3和tkin中使用colorchooser更改tkinter窗口中文本的颜色

2024-10-04 09:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

试着选择颜色然后打印它,打印位的工作只需要让颜色部分工作。如果你需要看到更多的代码,只要问。在

def mColour():
    color = colorchooser.askcolor()
    color_name = color[1]
    mlabel2 = Label(mGui,text=color).pack()
    messagebox.showinfo(title = "Colour",message = "This feature has not been fully added yet.")
    return
def mhello():
    mtext = ment.get()
    fg=color_name
    mlabel2 = Label(mGui,text=mtext).pack()
    return

错误:

^{pr2}$

Tags: 代码textnamereturn颜色deflabelpack
2条回答

在你的帮助下我找到了解决办法。在

#colour chooser
def mColour():
    color = colorchooser.askcolor()
    color_name = color[1]
    mlabel2 = Label(mGui,text=color).pack()
    messagebox.showinfo(title = "Colour",message = "This feature has not been fully added yet.")
    return color_name
#printing message 
def mhello():
    mtext = ment.get()
    mlabel2 = Label(mGui,text=mtext, fg = mColour()) # i put the fg and the mcolour inside here insted.
    mlabel2.pack()

据我所知,您正试图访问在mColour的局部范围内创建的变量(这意味着它不在mhello的范围内)。您可以通过使mColour返回color_name来解决此问题:

def mColour():
    color = colorchooser.askcolor()
    color_name = color[1]
    mlabel2 = Label(mGui,text=color).pack()
    messagebox.showinfo(title = "Colour",message = "This feature has not been fully added yet.")
    #################
    return color_name
    #################

然后访问mhello中的值,如下所示:

^{pr2}$

另外,我想谈两件事:

1)函数末尾的空return不起任何作用。在

2)pack方法返回None。您的代码应该如下所示:

mlabel2 = Label(mGui,text=mtext)
mlabel2.pack()

现在mlabel2指向标签。在

相关问题 更多 >