简体   繁体   English

如何更改笔工具的颜色?

[英]How can I Change My pen tools color?

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 我需要它来更新,因为有一个功能可以将var颜色更改为不同的颜色

You can use Tkinter's colorchooser to prompt the user to enter a color, then set it to the fill color of a line. 您可以使用Tkinter的colorchooser提示用户输入颜色,然后将其设置为线条的填充颜色。 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()   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM