【问题标题】:No way to color the border of a Tkinter Button?无法为 Tkinter 按钮的边框着色?
【发布时间】:2017-11-17 14:23:30
【问题描述】:

它适用于其他一些小部件,但不适用于按钮。

from Tkinter import *
root = Tk()
root.geometry("600x300+400+50")

btn_up = Button(root, text='Go UP')
btn_up.config(highlightbackground="red", highlightcolor="red", highlightthickness=10, relief=SOLID)
btn_up.pack()

root.mainloop()

Python 2.7 - Windows 10

【问题讨论】:

  • 它是错误的还是只是没有给它一个颜色的厚度?
  • 哦,我把bd=3弄错了,我把它拿出来。
  • 它只显示黑色边框,没有错误,没有颜色。
  • 另外,highlightthickness 不起作用,它只显示 2px 边框。
  • highlightthickness 不是边界。这是一个单独的事情。 Tkinter 不提供对边框颜色的控制。您是真的需要更改边框的颜色,还是只是想用特定颜色的线围绕它?换句话说,可能有一种方法可以做你想做的事,而不是你想做的事情。

标签: python button tkinter colors widget


【解决方案1】:

我使用的是 linux,当我运行你的代码时,我得到一个带有粗红色边框的按钮,所以看起来默认的 Windows 主题不支持highlightthickness,而默认的 linux 主题支持。

如果你想改变边框颜色,可以使用一些 ttk 主题,例如'clam':

from Tkinter import *
import ttk
root = Tk()

style = ttk.Style(root)
style.theme_use('clam')
style.configure('my.TButton', bordercolor="red")

ttk_button = ttk.Button(root, text='Go UP', style='my.TButton')
ttk_button.pack()

root.mainloop()

但是,使用style.configure('my.TButton', borderwidth=10) 更改边框宽度不会按预期增加红色边框的宽度。

【讨论】:

  • 如果是python 3.x,将Tkinter替换为tkinter,将import ttk替换为from tkinter import ttk。
猜你喜欢
  • 2018-12-22
  • 2023-01-28
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 2019-10-03
相关资源
最近更新 更多