【问题标题】:How do I change the colour of my button and label on Tkinter?如何在 Tkinter 上更改按钮和标签的颜色?
【发布时间】:2017-06-07 14:39:37
【问题描述】:

我的任务是在 Tkinter 上创建一个标签和按钮。按钮必须更改标签,我必须更改按钮和标签的颜色。我已经改变了背景的颜色,但我不知道如何为标签和按钮做同样的事情。

from tkinter import *
from tkinter import ttk

def change():
    print("change functon called")

def main():
    rootWindow = Tk()
    rootWindow.geometry('400x400')
    rootWindow.configure(bg="red")

    global Label
    label = ttk.Label( rootWindow, text="Hello World!" )
    label.pack()

    button1 = ttk.Button( rootWindow, text="Change Label",
                        command=change)
    button1.pack()

    rootWindow.mainloop()

main()

【问题讨论】:

  • 你知道你使用的是ttk标签还是tkinter标签吗?您是否阅读过小部件的文档?你搜索过这个网站吗?更改按钮属性有很多问题。

标签: python tkinter


【解决方案1】:

因此,在使用 tkinter 按钮 VS ttk 样式按钮时,配置按钮颜色会有些不同。

对于 tkinter 按钮,您可以使用 background = "color" 参数,如下所示:

button1 = Button( rootWindow, text="Change Label",
                      background = 'black', foreground = "white", command=change)

对于ttk 按钮,您可以配置样式,然后使用style = "style name" 参数,如下所示。

style = ttk.Style()
style.configure("BW.TLabel", foreground="white", background="black")

buttonTTK = ttk.Button( rootWindow, text="TTK BUTTON",style = "BW.TLabel", command=change)

有关ttk 配置的更多信息可以在here 找到

from tkinter import *
from tkinter import ttk

def change():
    print("change functon called")

def main():
    rootWindow = Tk()

    label = ttk.Label( rootWindow, text="Hello World!",
                       background = 'black', foreground = "white")
    label.pack()

    button1 = Button( rootWindow, text="Change Label",
                          background = 'black', foreground = "white", command=change)
    button1.pack()

    style = ttk.Style()
    style.configure("BW.TLabel", foreground="white", background="black")

    buttonTTK = ttk.Button( rootWindow, text="TTK BUTTON",style = "BW.TLabel", command=change)
    buttonTTK.pack()

    rootWindow.mainloop()

main()

结果:

【讨论】:

    【解决方案2】:

    在 Windows 10 上,我遇到了同样的问题,并找到了解决方案,虽然不是很满意。以下将创建一个具有白色前景类型的黑色按钮:

    先在标准TButton样式的基础上定义自定义按钮样式

    mystyle = ttk.Style()
    mystyle.configure('mycustom.TButton',background='black',foreground='white')
    

    然后使用新的自定义样式创建按钮

    mybutton = ttk.Button(root,style='mycustom.Tbutton')
    

    我说“不太令人满意”,因为这只有在我之前将整体主题设置为“默认”时才有效,如下所示:

    mystyle = ttk.Style()
    mystyle.theme_use('default')
    

    使用我系统上可用的任何其他主题(winnative、clam、alt、classic、vista 和 xpnative)只会将边框更改为黑色,并将背景保留为灰色。

    【讨论】:

    • 这对我也有用,但是你失去了漂亮的按钮并得到一个看起来不愉快的方形按钮。
    【解决方案3】:

    来自

    Python, Tkinter, Change of label color

    这将改变按钮的标签颜色:

    button1.configure(foreground="red")

    我假设标签可以使用相同的方法。

    【讨论】:

      【解决方案4】:

      对于 ttk 按钮,颜色控制的主要问题需要使用 ttk.Style() 和 theme_use。主题 alt/classic/default 允许 3D/彩色按钮控制。请参阅 Python: Changing ttk button color depending on current color? 以获取使用配置和映射样式的代码示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2019-11-24
        • 2014-04-16
        • 2016-11-07
        • 1970-01-01
        相关资源
        最近更新 更多