【发布时间】:2023-03-08 23:15:01
【问题描述】:
所以我想做一个按钮,当我点击它时,它会被删除并出现另一个,并且可以无限次这样做
a=0
button1 = [1,2,3,4,5,6]
def ButtonClick():
global a
#Destroys the first button
button1[a].destroy()
a+a+1
#Making new button, that should do the same as the old button
button1[a] = tk.Button(text='hello',command=ButtonClick)
canvas1.create_window(250+a*50, 140, window=button1[a])
button1[a] = tk.Button(text='hello',command=ButtonClick)
canvas1.create_window(100, 140, window=button1[a])
如您所见,新按钮也在使用 command=ButtonClick,所以当我按下创建的按钮时,它应该与旧按钮一样,但它没有,我不是确定为什么,因为当我更改新按钮上的命令时,它会显示错误,所以它以某种方式对 def ButtonClick 进行了裁判。但是当我按下新按钮时没有任何反应。谁能帮帮我?
【问题讨论】:
-
如果您想保留参考,请分配一个真实姓名和
append()它到列表中。 -
为什么要删除并重新创建一个按钮而不是重复使用它?您可以更改按钮的所有属性。销毁一个按钮并重新创建一个按钮而不是重用它似乎没有多大意义。
-
你知道你做了
a+a+1。我想你想要a = a+1。如果是这样的话a += 1更好
标签: python tkinter button tkinter-canvas