我是Tkinter的新成员,我正在尝试制作一个动画按钮。在
我使用的是enter-leave事件,但是click-on按钮的响应不是很好。在
我的代码是:
imagePath = "Resources/"
imagelist = ["boton_1.gif","boton_2.gif","boton_3.gif","boton_4.gif","boton_5.gif","boton_6.gif",
"boton_7.gif","boton_8.gif","boton_9.gif","boton_10.gif","boton_11.gif","boton_12.gif",
"boton_13.gif","boton_14.gif","boton_15.gif","boton_16.gif"]
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=imagePath+imagefile)
giflist.append(photo)
self.photo=giflist[0]
button = Button(buttonFrame, image=self.photo,background='orange',activebackground='lightsalmon',
command=lambda: controller.show_frame(ListPlayerPage))
button.pack(pady=5)
def enter(event):
self.clickOnButton1 = True
for i in range(1,8):
button.config(image=giflist[i])
button.update()
time.sleep(0.1)
if self.clickOnButton1 == False:
break
while (self.clickOnButton1):
for i in range (9,15):
button.config(image=giflist[i])
button.update()
time.sleep(0.08)
if self.clickOnButton1 == False:
break
def leave(event):
self.clickOnButton1 = False
button.config(image=self.photo)
button.update()
button.bind("<Enter>",enter)
button.bind("<Leave>",leave)
谢谢!!在
我以布莱恩·奥克利为例,找到了一个很好的解决方案!在
首先,这是一个有点复杂动画的动画按钮。我有16张照片。第一个是基本图像。然后我有八个图像,它们是动画的第一部分。其余的图像是动画的循环部分。在
将鼠标放在按钮上时,动画开始。在
这是密码!公司名称:
谢谢!!!在
部分问题肯定与您调用
sleep
有关。作为一个好的经验法则,您不应该在GUI的主线程中调用sleep
。它阻止GUI处理所有事件,包括屏幕刷新。在一般来说,您还应该避免调用
update
。如果在update
的处理过程中最后调用了一个再次调用update
的方法,则可能会导致嵌套的事件循环。在下面是一个非常简单的解决方案示例,它创建了一个可以设置动画的按钮。{cde>每隔一个字符串对一个新的字符串进行迭代。这个例子将永远保持动画效果,但是你可以很容易地让它只显示一次每个项目。这将修改文本以使示例变短,但您可以轻松地修改它以更改图像而不是文本。在
您可以像任何其他按钮一样使用它,但可以调用
^{pr2}$animate
来启动动画,并调用cancel_animate
来取消它:相关问题 更多 >
编程相关推荐