我想创建一个程序,用户可以通过单击鼠标创建不同的按钮,这些按钮应该是独立的。使用这个逻辑,用户可以创建一个可以工作的复选按钮,当被选中时从绿色变为红色。我的问题是,如果用户再次单击鼠标,则检查按钮会移动,而不是创建新的检查按钮。有什么建议怎么做吗?
from tkinter import *
root = Tk()
button1 = IntVar()
def color_checkbutton(): # define the colors of the checkbutton
if button1.get() == 1:
example_checkbutton.configure(bg='red')
else:
example_checkbutton.configure(bg='green')
example_checkbutton = Checkbutton(root, variable=button1, textvariable=button1, command=color_checkbutton)
def place_checkbutton_in_canvas(e): # order to insert the checkbutton
xx_and = e.x
yy_and = e.y
example_checkbutton.place(x=xx_and, y=yy_and)
root.bind('<Button-1>', place_checkbutton_in_canvas)
root.mainloop()