【发布时间】:2020-06-25 11:45:53
【问题描述】:
我尝试了一些我在 Stackoverflow 上找到的东西,例如在按钮周围放置一个框架并为其赋予颜色,就像 here 所说的那样。我还尝试了一些其他的东西,据说是here,但我无法让它工作。
我使用的是 Mac OS,按钮是圆形的,但它周围有一个正方形,使它看起来不太好。有谁知道我怎样才能让这个正方形改变它的颜色?
这是我正在使用的代码:
empty = Button(frame, text='Opnieuw', command=clear, font=bold_font)
empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)
empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))
这就是我所说的正方形:按钮周围的白色正方形,没有那种绿色。
添加 Bryan Oakley 所说的内容后,通过执行以下操作:
empty = Button(frame, text='Opnieuw', command=clear, font=bold_font)
empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)
empty.configure(highlightbackground="#009688")
empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))
更具体地说,这是我正在使用的一大段代码:
from tkinter import *
from tkinter import font as tkfont
root = Tk()
root.config(background='#009688')
root.title('Contractmaker')
# GUI stuff that takes care of the scrollbar
def on_configure(event):
canvas.configure(scrollregion=canvas.bbox('all'))
def on_mousewheel(event):
canvas.yview_scroll(int(event.delta), 'units')
# Create some fonts
bold_font = tkfont.Font(weight='bold')
# Create the actual GUI
canvas = Canvas(root, width=450, height=550)
canvas.config(background='#009688')
canvas.pack(side=RIGHT)
scrollbar = Scrollbar(root, command=canvas.yview)
# scrollbar.pack(side=RIGHT, fill='y')
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind('<Configure>', on_configure)
canvas.bind_all('<MouseWheel>', on_mousewheel)
frame = Frame(canvas)
frame.config(background='#009688')
canvas.create_window((0,0), window=frame)
empty = Button(frame, text='Opnieuw', font=bold_font)
empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)
empty.configure(highlightbackground='#009688')
empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))
root.mainloop()
这是我得到的:
有谁知道我怎样才能让按钮的白色部分保持白色而不是改变它的颜色?我正在使用 python 3.8 和 Tkinter 8.6。
【问题讨论】:
-
我无法重现您看到的问题。对我来说,代码根据文档工作,只调整按钮周围的环。请创建一个完整的minimal reproducible example,以便我们确保您没有遗漏详细信息。我猜它应该需要不到十几行代码。另外,我们需要知道您使用的是哪个版本的 python 和 tkinter。
-
我再次更新了我的问题。我希望我提供了所需的信息。期待您的回答,谢谢!
-
我不确定,但这是一个错误,如果您将注意力集中在主窗口上,它就会消失。例如,尝试在同一程序中打开一个
Toplevel窗口,然后单击顶层窗口,然后返回主窗口。 -
我仍然在我的 mac 上看到一个白色按钮,虽然我正在运行 python 3.7 和 tkinter 8.5。
-
我正在使用 PyCharm。我实际上不知道我是否正在使用任何环境,所以我不认为我是。我看了你建议的页面,但我不太明白他们在说什么。我想我会提出一个关于我的 GUI 没有在视觉上集中的新问题。非常感谢您的时间和回答!
标签: python button tkinter colors border