使用python 3和tkinter中的colorchooser更改tkinter窗口中文本的颜色
试着选择打印机的颜色,然后打印出来,只需要让颜色部分正常工作,打印就可以了。如果您需要查看更多代码,请询问使用python 3和tkinter中的colorchooser更改tkinter窗口中文本的颜色,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,试着选择打印机的颜色,然后打印出来,只需要让颜色部分正常工作,打印就可以了。如果您需要查看更多代码,请询问 def mColour(): color = colorchooser.askcolor() color_name = color[1] mlabel2 = Label(mGui,text=color).pack() messagebox.showinfo(title = "Colour",message = "This feature has not be
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
据我所知,您正在尝试访问在
mColour
的本地范围内创建的变量(这意味着它不在mhello
的范围内)。您可以通过使mColour
返回color\u 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()
此外,我想谈两件事:
1) 函数末尾的裸返回
不起任何作用
2) pack
方法返回None
。您的代码应该如下所示:
mlabel2 = Label(mGui,text=mtext)
mlabel2.pack()
现在
mlabel2
像应该的那样指向标签。在您的帮助下,我找到了解决方案
#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()
它现在说我的颜色不是defined@Minigeek22-您将函数命名为
mColour
,而不是myColor
。它现在只发布颜色代码和我想要打印的单词,但仍然是黑色。(仍然)同样的问题是,它以黑色打印颜色代码和单词。