【发布时间】:2017-06-15 20:13:48
【问题描述】:
编辑
我使用图片作为按钮。第三张图片是“开始”按钮,第四张图片是“小键盘”按钮。
按钮占用的空间比图片多,正如您在第一张图片中看到的那样。我想拿走那个空间,所以只占用图像空间。
我想去掉边框占用的空间,因为我希望按钮只占用图像大小的空间。
第一个图像在 MainButton 类中有这行代码:
self.configure(bg=color['background'], activebackground=color['background'], borderwidth=0, highlightthickness=0)
第二张图片在 MainButton 类中有这行代码:
self.configure(bg=color['background'], activebackground=color['background'])
完整代码:
class MainButton(Button):
def __init__(self, *args, **kwargs):
Button.__init__(self, *args, **kwargs)
self.helv20 = Font(root= self,family="Arial",size=18)
self.helv18 = Font(root= self,family="Arial",size=16)
self.configure(bg=color['background'], activebackground=color['background'], borderwidth=0, highlightthickness=0)
def _active(self, event):
self.configure(image=self.ActiveImage)
def _inactive(self, event):
self.configure(image=self.DefaultImage)
class RunButton(MainButton):
def __init__(self, *args, **kwargs):
MainButton.__init__(self, *args, **kwargs)
self.bind("<ButtonPress>", self._active)
self.bind("<ButtonRelease>", self._inactive)
self.DefaultImage=PhotoImage(file="img/mainbutton_green.png")
self.ActiveImage=PhotoImage(file="img/mainbutton_active.png")
self.configure(image=self.DefaultImage, compound=CENTER, font=self.helv20)
【问题讨论】:
-
不清楚你在问什么。您写了“第一个图像在 MainButton 类中有这行代码...borderwidth=0,highlightthickness=0 ...”。该代码是否给了您想要的东西?此外,您的图像似乎颠倒了。如果您关闭边框和高光厚度,第二张图像是我所期望的。另外,第三个(绿色矩形)和第四个(灰色矩形)图像是干什么用的?最后,这些是 tkinter 按钮还是 ttk 按钮?
-
你不能使用
width和height来设置按钮大小吗?你也可以试试padx和pady。 -
@BryanOakley 编辑了我的帖子。这些按钮占用的空间超出了它们的需要。这些按钮是 tkinter 按钮。
-
我在第二张图片中看不到任何多余的空间,当我运行您的代码时,我看不到任何多余的空间。
标签: python image button tkinter