如何在python中更改Tkinter按钮的on_press“动画”

问题描述 投票:1回答:2

i具有类似任务栏的Frame,其中包含带有图像的自定义Button。但是,每次我单击该按钮时,Tkinter都会将按钮向右/臀部移动1px。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8ybzl0ci5wbmcifQ==” alt =“单击按钮”>“>

是否有可能忽略此行为?还是我必须从Tkinter.Label而不是Tkinter.Button派生?

edit:

添加一些代码:进口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

我有一个类似于任务栏的框架,其中包含带有图像的自定义按钮。但是,每次我单击该按钮时,Tkinter都会将按钮1px移至右侧/底部。是否可以覆盖此...

python user-interface tkinter customization
2个回答
1
投票

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


0
投票

您可以通过在小部件的绑定中返回“ break”来禁用按钮的动画,这将阻止绑定函数的传播。

© www.soinside.com 2019 - 2024. All rights reserved.