【问题标题】:How to change on_press "animation" of Tkinter button in python如何在python中更改Tkinter按钮的on_press“动画”
【发布时间】:2015-06-18 12:21:17
【问题描述】:

我有一个类似任务栏的Frame,其中包含带有图像的自定义Buttons。但是每次我点击这个按钮时,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?图片没试过,但是SUNKEN按钮在点击时似乎没有移动。
  • 给你。我正在使用relief 样式"flat"。我也试过sunken,但我没有看到任何区别。当我用relief="sunken" 初始化Button 时,宽度空间一直处于启用状态。如果我使用Label,动画就消失了,但是我不能禁用标签,字体也不会改变。
  • 您似乎在on_pressed 中将浮雕设置为flat,因此您可能也必须将其删除(或者将其更改为在那个地方下沉,如果您还没有的话)。但是下沉可能看起来不太好,按钮周围有那个边框。丑陋的 hacky 替代方案:制作“激活”图像,使其向左上角移动 1px :-P

标签: python user-interface tkinter customization


【解决方案1】:

不确定这是否适用于您的专用按钮,但单击时按钮的移动方式似乎取决于它的 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,因此您可能必须全部更改才能使其生效。

【讨论】:

  • 我已经尝试过这种方法,这只有在你有默认背景色的情况下才有效。例如,如果您将根背景更改为黑色(root.config(background="black"))并将按钮背景更改为黑色(Button(root,background="black",...)),它似乎可以工作,但仅在您单击按钮之前,背景颜色将再次恢复默认值。
  • @VRage 你试过在按钮中设置activebackground="black"吗?
  • 不,我没有,这确实有效。维伦丹克。不知何故,我在文档中忽略了这个选项。您能否将这些选项添加到您的示例代码中。
【解决方案2】:

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

因此,您可以更改通常绑定到按钮的函数以返回“break”。

或者您可以添加另一个绑定,但这确实会阻止在此之后进行的任何绑定:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    相关资源
    最近更新 更多