簡體   English   中英

使用python 3和tkinter中的colorchooser更改tkinter窗口中文本的顏色

[英]Changing color of text in a tkinter window by using colorchooser in python 3 and 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 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_name來解決此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訪問該值, mhello所示:

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

另外,我想談兩件事:

1)函數末尾的裸return什么都不做。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM