如何在 python 中更改 on_press "animation" 的 Tkinter 按钮
How to change on_press "animation" of Tkinter button in python
我有一个类似任务栏的 Frame
,其中包含带图像的自定义 Button
。但是每次我点击这个按钮时,Tkinter 都会将按钮移动 1px 到 right/buttom.
是否可以覆盖此行为?或者我必须从 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
,因此您可能需要全部更改它们才能生效。
您可以通过在小部件的绑定中 returning "break" 来禁用按钮的动画,这会停止绑定函数的传播。
因此,您可以将通常绑定到按钮的功能更改为 return "break"。
或者您可以添加另一个绑定,但这会阻止在此之后进行的任何绑定:
tkButton.bind("<Button-1>", lambda _: "break", add=True)
我想我找到了某种使用浮雕和边框的解决方案:
closebut = Button(title, text="X", relief=SUNKEN, bd=0, command=close)
closebut.pack(side=RIGHT)
你可以观察到我使用 relief = SUNKEN
然后 bd = 0
在我的按钮上获得了一个很好的平面效果!
我有一个类似任务栏的 Frame
,其中包含带图像的自定义 Button
。但是每次我点击这个按钮时,Tkinter 都会将按钮移动 1px 到 right/buttom.
是否可以覆盖此行为?或者我必须从 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
,因此您可能需要全部更改它们才能生效。
您可以通过在小部件的绑定中 returning "break" 来禁用按钮的动画,这会停止绑定函数的传播。
因此,您可以将通常绑定到按钮的功能更改为 return "break"。
或者您可以添加另一个绑定,但这会阻止在此之后进行的任何绑定:
tkButton.bind("<Button-1>", lambda _: "break", add=True)
我想我找到了某种使用浮雕和边框的解决方案:
closebut = Button(title, text="X", relief=SUNKEN, bd=0, command=close)
closebut.pack(side=RIGHT)
你可以观察到我使用 relief = SUNKEN
然后 bd = 0
在我的按钮上获得了一个很好的平面效果!