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()
Colour=newcol.get()definesColourin local scope. TheColourin global scope remains the same..after()that takes only one parameter is essentially useless - it's equivalent totime.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.