Question

from tkinter import *
tk = Tk()

tk.title("Kaint")
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
color = input("What Color?")
size = float(input("Size?"))
def cc(event):
    color = input("What Color?")
def paint(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))
    x1 = event.x - size
    y1 = event.y - size
    x2 = event.x + size
    y2 = event.y + size
canvas.create_oval(x1, y1, x2, y2, fill=color, outline=color)
canvas.bind_all("<Button-3>", cc)
canvas.bind_all("<B1-Motion>", paint)
while True:
    tk.update_idletasks()
    tk.update()
mainloop()    

How Do i change my pen tools color(Paint function)? I need it to update because a have a function that changes the var color to a different color

Was it helpful?

Solution

You can use Tkinter's colorchooser to prompt the user to enter a color, then set it to the fill color of a line. Here's an example:

from tkinter import *

def chooseColor(event):
    global color #set color to global so it updates in other function
    color = colorchooser.askcolor()

def paint(event):
    #create a line while in this event. use color[1] to get the second element in the color tuple
    canvas.create_line(event.x,event.y,event.x+1,event.y+1, fill=color[1])

tk = Tk()

canvas = Canvas(tk, width=500, height=500)
canvas.pack()

color = tkColorChooser.askcolor() #get initial color, as a tuple

canvas.bind_all("<Button-3>", chooseColor)
canvas.bind_all("<B1-Motion>", paint)

tk.mainloop()   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top