我有一个将检查按钮粘贴到画布中的代码。如果单击,它们也会改变颜色。如果我单击从一开始就设置的另一个检查按钮,我希望能够在我通过鼠标单击选择的 2 个检查按钮之间画一条线。当我单击第一个复选按钮时,我还需要线条来改变颜色。有什么建议怎么做吗?
from tkinter import *
root = Tk()
buttons = []
class CMD: #Auxilliary function for callbacks using parameters. Syntax: CMD(function, argument1, argument2, ...)
def __init__(s1, func, *args):
s1.func = func
s1.args = args
def __call__(s1, *args):
args = s1.args+args
s1.func(*args)
def color_checkbutton(pos=0): # define the colors of the checkbutton
if buttons[pos][0].get() == 1:
buttons[pos][2].configure(bg='red')
else:
buttons[pos][2].configure(bg='green')
def place_checkbutton_in_canvas(e): # order to insert the checkbutton
if len(str(e.widget))<3: ## Don't place a new one if a checkbox was clicked
b = IntVar()
pos = len(buttons)
xx_and = e.x
yy_and = e.y
buttons.append([b,pos, Checkbutton(root, variable=b, textvariable=b, command=CMD(color_checkbutton,pos))])
buttons[-1][2].place(x=xx_and, y=yy_and)
color_checkbutton(pos)
root.bind('<Button-1>', place_checkbutton_in_canvas)
line = IntVar()
draw_line_check = Checkbutton(root, variable=line)
draw_line_check.place(x=30,y=100)
root.mainloop()