Python的Tkinter输入甚至

2024-10-04 11:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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)

谢谢!!在


Tags: inimageselfconfigfalseforupdatebutton
2条回答

我以布莱恩·奥克利为例,找到了一个很好的解决方案!在

首先,这是一个有点复杂动画的动画按钮。我有16张照片。第一个是基本图像。然后我有八个图像,它们是动画的第一部分。其余的图像是动画的循环部分。在

将鼠标放在按钮上时,动画开始。在

这是密码!公司名称:

import Tkinter as tk # use tkinter for python 3.x

root = tk.Tk()
root.geometry("300x200")

class AnimatedButton(tk.Button):

    def __init__(self, *args, **kwargs):
        tk.Button.__init__(self, *args, **kwargs)
        self._job = None
        self.i = 1

    def cancel_animation(self,image):
        self.configure(image=image)
        self.i = 1
        if self._job is not None:
            self.after_cancel(self._job)
            self._job = None


    def animate(self, imagelist):
        image = imagelist[self.i]
        self.i+=1
        if self.i == (len(imagelist)-1):
            self.i = 9
        self.configure(image=image)
        self._job = self.after(80, self.animate, imagelist)


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 = tk.PhotoImage(file=imagePath+imagefile)
    giflist.append(photo)

image = giflist[0]
button = AnimatedButton(root,image = image)
button.bind("<Enter>", lambda event: button.animate(giflist))
button.bind("<Leave>", lambda event: button.cancel_animation(image))

button.pack()

root.mainloop()

谢谢!!!在

部分问题肯定与您调用sleep有关。作为一个好的经验法则,您不应该在GUI的主线程中调用sleep。它阻止GUI处理所有事件,包括屏幕刷新。在

一般来说,您还应该避免调用update。如果在update的处理过程中最后调用了一个再次调用update的方法,则可能会导致嵌套的事件循环。在

下面是一个非常简单的解决方案示例,它创建了一个可以设置动画的按钮。{cde>每隔一个字符串对一个新的字符串进行迭代。这个例子将永远保持动画效果,但是你可以很容易地让它只显示一次每个项目。这将修改文本以使示例变短,但您可以轻松地修改它以更改图像而不是文本。在

import Tkinter as tk # use tkinter for python 3.x
class AnimatedButton(tk.Button):
    def __init__(self, *args, **kwargs):
        tk.Button.__init__(self, *args, **kwargs)
        self._job = None

    def cancel_animation(self):
        if self._job is not None:
            self.after_cancel(self._job)
            self._job = None

    def animate(self, textlist):
        text = textlist.pop(0)
        textlist.append(text)
        self.configure(text=text)
        self._job = self.after(500, self.animate, textlist)

您可以像任何其他按钮一样使用它,但可以调用animate来启动动画,并调用cancel_animate来取消它:

^{pr2}$

相关问题 更多 >