简体   繁体   English

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

[英]How to change on_press "animation" of Tkinter button in python

i have a taskbar-like Frame , which contains custom Button s with images.我有一个类似任务栏的Frame ,其中包含带有图像的自定义Button But everytime i click on this button, Tkinter displaced the button 1px to the right/buttom.但是每次我点击这个按钮时,Tkinter 都会将按钮向右/按钮移动 1px。

单击按钮

Is it possible to override this behaviour?是否可以覆盖此行为? Or do i have to derived from Tkinter.Label instead of Tkinter.Button ?或者我必须从Tkinter.Label而不是Tkinter.Button

edit: Adding some code: import Tkinter import logging编辑:添加一些代码: import Tkinter import logging

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

Not sure whether this works with your specialized button, but how the button moves when it's clicked seems to depend on it's relief style .不确定这是否适用于您的专用按钮,但是单击按钮时按钮的移动方式似乎取决于它的 浮雕样式 With relief=SUNKEN , the button seems not to move at all when clicked, and with borderwidth=0 it appears to be indistinguishable from a FLAT button.使用relief=SUNKEN ,单击时按钮似乎根本不移动,而使用borderwidth=0它似乎与FLAT按钮无法区分。

Minimal example:最小的例子:

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

Note that you set and re-set the relief to FLAT multiple times in your code, so you might have to change them all for this to take effect.请注意,您在代码中多次将relief设置并重新设置为FLAT ,因此您可能需要全部更改它们才能生效。

You can disable the animation of a button by returning "break" in the widget's bind, which stops the propagation of bound functions.您可以通过在小部件的绑定中返回“break”来禁用按钮的动画,这会停止绑定函数的传播。

So you can either alter the function you normally have bound to the button to return "break".因此,您可以更改通常绑定到按钮的功能以返回“中断”。

Or you can add another bind, this does however prevent any binds that are made after this one:或者您可以添加另一个绑定,但这会阻止在此绑定之后进行的任何绑定:

tkButton.bind("<Button-1>", lambda _: "break", add=True)

I think I found some kind of a solution using relief and border:我想我找到了某种使用浮雕和边框的解决方案:

closebut = Button(title, text="X", relief=SUNKEN, bd=0, command=close)
closebut.pack(side=RIGHT)

You can observe that I used relief = SUNKEN and then bd = 0 to get a nice FLAT effect on my button!你可以观察到我使用relief = SUNKEN然后bd = 0在我的按钮上获得了一个很好的 FLAT 效果!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM