【问题标题】:How do I remove the border of a Image Button in Tkinter?如何在 Tkinter 中删除图像按钮的边框?
【发布时间】:2022-11-26 13:35:37
【问题描述】:

我知道如何删除 Tkinter 按钮和图像的边框。它的完成方式与您为其他所有事情所做的方式几乎完全一样

borderwidth=0

为什么我需要帮助,即使我把它放在小部件的“设计参数”中,它仍然有一个边框。

我的代码如下。

# Imports the tkinter library.
from tkinter import *
from tkmacosx import Button


selectedBackground = "black"
selectedForeground = "#22fd35"

root = Tk()
root.configure(bg=selectedBackground)

def openCipher():
    print("open cipher")


def openDecipher():
    print("open decipher")

cipherButton = Button(root, text="  Cipher  ", padx=40, pady=20, command=openCipher, borderwidth=0, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground)
cipherButton.grid(row=1, column=0)
decipherButton = Button(root, text="Decipher", padx=40, pady=20, command=openDecipher, borderwidth=0, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground).grid(row=1, column=2)
spacer1 = Label(root, text="     ", padx=10, pady=1, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground).grid(row=4, column=1)
quitButton = Button(root, text="Exit d3cryptt", padx=10, pady=5, command=root.quit, borderwidth=0, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground).grid(row=5, column=1)
spacer2 = Label(root, text="     ", padx=10, pady=1, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground).grid(row=6, column=1, pady=30)

# changecolour = Button(root, text="change colour", padx=1, pady=5, background=selectedBackground, foreground=selectedForeground, highlightbackground=selectedForeground, command=lambda: changeColour(selectedBackground3, selectedForeground3)).grid(row=7, column=0)
theme1 = PhotoImage(file = "/Documents/theme1button.png")
theme1Button = Button(root, image=theme1, borderwidth=0, background=selectedBackground, command=openCipher)
theme1Button.place(x=50, y=100)

#Enter the event main loop
root.mainloop()

如果您想自己测试一下,这是按钮的图像。 https://i.stack.imgur.com/OzB58.png

图像出现在屏幕上时周围有边框,即使 borderwidth = 0,如下图所示。

我不确定关于如何解决此问题的任何其他解决方案。我什至尝试将其从 .place 更改为 .grid,但它仍然有边框。

可能是因为它不在画布上,但如果这是问题所在,我需要有人为我澄清。如果他们能指导我如何操作,或者提供有关如何操作的有用视频,我将不胜感激。

我很感激给出的任何建议。

【问题讨论】:

  • 如果您只放置没有按钮的图像会怎样?
  • @AnthonyL 没有边界。
  • 它看起来就像从填充或边距向我显示的背景。不过我确实放大了它们,那些是一些粗的 0 宽度边框

标签: python tkinter tkinter-button tkinter-label


【解决方案1】:

尝试突出显示厚度:

theme1Button = Button(root, image=theme1, borderwidth=0, highlightthickness=0, background=selectedBackground, command=openCipher)

【讨论】:

    【解决方案2】:

    这是因为 MacOs。我遇到了类似的问题;唯一的解决方法是改用标签,然后将点击事件与函数绑定。 你可以这样做:

    theme1Label = Label(root, image=theme1, borderwidth=0, background=selectedBackground)           
    theme1Label.bind('<Button>', openCipher)
    theme1Label.place(x=50, y=100)
    

    然后在函数中传递“事件”参数:

    def openCipher(event):
        print("open cipher")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2017-06-15
      • 2011-06-23
      • 2017-10-12
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多