Minigeek22
Minigeek22

Reputation: 99

Changing color of text in a tkinter window by using colorchooser in python 3 and tkinter

Try to choose the color of the then print it, the printing bit works just need to get the color part working. If you need to see more of the code just ask.

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

Error:

color_name not defined

Upvotes: 1

Views: 1406

Answers (2)

Minigeek22
Minigeek22

Reputation: 99

I have found the solution with your help.

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

Upvotes: 1

user2555451
user2555451

Reputation:

From what I can tell, you are trying to access a variable that was created within the local scope of mColour (which means it isn't in mhello's scope). You can fix this by making mColour return 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
    #################

And then accessing that value in mhello like so:

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

Also, I would like to address two things:

1) A bare return at the end of a function does nothing.

2) The pack method returns None. Your code should look like this:

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

Now mlabel2 points to the label like it should.

Upvotes: 2

Related Questions