0

so i'm very new to coding, i took a tkinter draw program off the internet and wanted to add a change colour function by opening up a new window, having the user input the hex code, then having the window close. However the window isn't even opening up when i add the after function, and just waits 5 seconds showing the button being depressed.

from tkinter import *


root = Tk()

# Create Title
root.title( "Paint App ")

# specify size
root.geometry("500x350")

#set colour
Colour = "#000000"


def changecolour():
    col=Tk()
    Label(col, text= "Enter the new hex code, you have 5 seconds ").grid(column=1,row=0)
    newcol=Entry(col,width=15)
    newcol.grid(column=1,row=1)
    col.after(5000)
    Colour=newcol.get()
    col.destroy()
    

# define function when
# mouse double click is enabled
def paint( event ):
    
    # Co-ordinates.
    x1, y1, x2, y2 = ( event.x - 5 ),( event.y - 5 ), ( event.x + 5 ),( event.y + 5 )
    
    # specify type of display
    w.create_oval( x1, y1, x2,y2, fill = Colour )


# create canvas widget.
w = Canvas(root, width = 400, height = 250)

# call function when double
# click is enabled.
w.bind( "<B1-Motion>", paint )
# create label.
l = Label( root, text = "Click and Drag slowly to draw." )
l.pack()
w.pack()

btn = Button(root, text = 'Change colour', bd = '5', command = changecolour)
btn.place(x= 150,y=300)

mainloop()
3
  • Colour=newcol.get() defines Colour in local scope. The Colour in global scope remains the same. Commented Feb 21, 2022 at 19:03
  • 2
    The form of .after() that takes only one parameter is essentially useless - it's equivalent to time.sleep(), which you will find numerous answers warning against it here because all it does is lock up the GUI for the specified amount of time. You want the form with two or more parameters, that specifies both a duration, and a function to be called after that duration. The idea is that you call .after(..., ...) and return to the mainloop so that the GUI remains responsive; the mainloop will then call the specified function later. Commented Feb 21, 2022 at 19:27
  • Why do you require the user to input the color hex code in 5 seconds? Commented Feb 22, 2022 at 1:14

1 Answer 1

1

Use the built-in colorchooser;

You can try it by running the following command:

python -m tkinter.colorchooser

Use it in your program like this:

from tkinter import colorchooser

def changecolour():
     global Colour
    _, Colour = colorchooser.askcolor(title ="Choose color")

The askcolor function returns two values;

  • An RGB tuple
  • A hex string starting with a #.

Since you only need the latter, I used _, Colour. Also, seeing as you want to change the global variable Colour, you have to mark it as such in changecolour.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.