如何更改笔工具的颜色?

时间:2014-04-08 13:41:06

标签: python function canvas tkinter pen

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

如何更改笔工具颜色(绘画功能)? 我需要它来更新,因为有一个函数可以将var颜色更改为不同的颜色

1 个答案:

答案 0 :(得分:3)

您可以使用Tkinter colorchooser提示用户输入颜色,然后将其设置为一条线的填充颜色。这是一个例子:

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