1

尝试选择颜色然后打印,打印位工作只需要让颜色部分工作。如果您需要查看更多代码,请询问。

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

错误:

color_name not defined
4

2 回答 2

2

据我所知,您正在尝试访问在本地范围内创建的变量mColour(这意味着它不在mhello的范围内)。您可以通过mColourreturn解决此问题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

def mhello():
    mtext = ment.get()
    ############
    fg=mColour()
    ############
    mlabel2 = Label(mGui,text=mtext).pack()

另外,我想说明两点:

return1)一个函数末尾的一个bare什么都不做。

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

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

现在mlabel2指向应有的标签。

于 2013-10-18T16:03:29.823 回答
1

在您的帮助下,我找到了解决方案。

#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()
于 2013-10-18T16:24:57.393 回答