【发布时间】: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