Reputation: 1
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()
Upvotes: 0
Views: 320
Reputation: 43495
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;
#
.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
.
Upvotes: 1