如何在PYTHON中更改Tkinter按钮的按下动画 [英] How to change on_press "animation" of Tkinter button in python

查看:0
本文介绍了如何在PYTHON中更改Tkinter按钮的按下动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似任务栏的Frame,它包含带有图像的自定义Button。但每次我点击这个按钮,Tkinter就会把按钮向右移动1px/按钮。

是否可以覆盖此行为?或者我必须派生自Tkinter.Label而不是Tkinter.Button

编辑: 添加一些代码: 导入Tkinter 导入日志记录

logger = logging.getLogger(__name__)
class DesktopBtn(Tkinter.Button):
    '''
    Represents a Button which can switch to other Desktops
    '''

    _FONTCOLOR="#FFFFFF"

    def getRelativePath(self,folder,name):
        import os
        dir_path = os.path.dirname(os.path.abspath(__file__))
        return os.path.abspath(os.path.join(dir_path, '..', folder, name))

    def __init__(self, parent,desktopManager,buttonName, **options):
        '''
        :param buttonName: Name of the button

        '''
        Tkinter.Button.__init__(self, parent, **options)
        logger.info("init desktop button")
        self._imagePath=self.getRelativePath('res','button.gif')
        self._BtnPresspath = self.getRelativePath('res','buttonP.gif')
        self._BtnPressImage = Tkinter.PhotoImage(file=self._BtnPresspath)
        self._image = Tkinter.PhotoImage(file=self._imagePath)
        self.bind('<ButtonPress-1>',self._on_pressed)
        self.bind('<ButtonRelease-1>',self._on_release)
        self._parent = parent
        self._btnName = buttonName
        self._desktopManager = desktopManager
        self.config(width=70, height=65,borderwidth=0,compound=Tkinter.CENTER,font=("Arial", 9,"bold"),foreground=self._FONTCOLOR, text=buttonName,wraplength=64,image=self._image, command=self._onClickSwitch)

    def _on_pressed(self,event):
        self.config(relief="flat")
        self.config(image=self._BtnPressImage)

    def _on_release(self,event):
        self.config(image=self._image)

    def _onClickSwitch(self):
        self.config(relief="flat")
        logger.info("Buttonclickmethod onClickSwitch")
        self._desktopManager.switchDesktop(self._btnName)

    def getButtonName(self):
        return self._btnName

推荐答案

不确定这是否适用于您的专用按钮,但按钮在单击时的移动方式似乎取决于它的relief style。对于relief=SUNKEN,按钮在被单击时似乎完全不动,而对于borderwidth=0,它似乎与FLAT按钮没有区别。

最小示例:

root = Tk()
image = PhotoImage(file="icon.gif")
for _ in range(5):
    Button(root, image=image, borderwidth=0, relief=SUNKEN).pack()
root.mainloop()

请注意,您在代码中多次将relief设置和重新设置为FLAT,因此可能必须全部更改才能使其生效。

这篇关于如何在PYTHON中更改Tkinter按钮的按下动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆