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

2024-09-29 17:11:54 发布

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

我有一个类似Frame的任务栏,其中包含自定义的带有图像的Button。但是每次我点击这个按钮,Tkinter就会把按钮移到右边。在

Button clicked

有没有可能超越这种行为?{cd3>而不是cd3}?在

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

Tags: pathnameimageselfconfigosontkinter
1条回答
网友
1楼 · 发布于 2024-09-29 17:11:54

不确定这是否适用于您的专用按钮,但按钮在单击时如何移动似乎取决于它的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()

请注意,您在代码中多次设置并重新设置reliefFLAT,因此您可能必须更改它们才能生效。在

相关问题 更多 >

    热门问题