如何在 python 中单击鼠标创建新的复选按钮
how to create new checkbuttons with a mouse click in python
我想创建一个程序,用户可以通过单击鼠标创建不同的按钮,这些按钮应该是独立的。有了这个逻辑,用户可以创建一个有效的复选按钮,当它被选中时从绿色变为红色。我的问题是,如果用户再次单击鼠标,复选按钮会移动,而不是创建新的复选按钮。有什么建议吗?
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()
你只有一个 example_checkbutton。每当您调用 .place()
方法时,此按钮都会移动。
如果您想要新的,只需将它们创建为新的即可 Checkbox-widgets:
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
xx_and = e.x
yy_and = e.y
Checkbutton(root, variable=button1, textvariable=button1, command=color_checkbutton).place(x=xx_and, y=yy_and)
这将创建新的复选按钮,它们都链接到 button1
变量。
编辑:
如果您想要新的复选按钮,则必须维护一个 IntVar() 和 Checkbutton() 对象列表,每次单击该列表都会变长。下面的代码应该可以工作。我还在创建时执行颜色更改,将它们创建为绿色和红色。
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)
root.mainloop()
我想创建一个程序,用户可以通过单击鼠标创建不同的按钮,这些按钮应该是独立的。有了这个逻辑,用户可以创建一个有效的复选按钮,当它被选中时从绿色变为红色。我的问题是,如果用户再次单击鼠标,复选按钮会移动,而不是创建新的复选按钮。有什么建议吗?
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()
你只有一个 example_checkbutton。每当您调用 .place()
方法时,此按钮都会移动。
如果您想要新的,只需将它们创建为新的即可 Checkbox-widgets:
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
xx_and = e.x
yy_and = e.y
Checkbutton(root, variable=button1, textvariable=button1, command=color_checkbutton).place(x=xx_and, y=yy_and)
这将创建新的复选按钮,它们都链接到 button1
变量。
编辑:
如果您想要新的复选按钮,则必须维护一个 IntVar() 和 Checkbutton() 对象列表,每次单击该列表都会变长。下面的代码应该可以工作。我还在创建时执行颜色更改,将它们创建为绿色和红色。
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)
root.mainloop()