如何更改 Tkinter 上按钮和标签的颜色?

编程入门 行业动态 更新时间:2024-10-24 17:27:03
本文介绍了如何更改 Tkinter 上按钮和标签的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

from tkinter import *从 tkinter 导入 ttk定义更改():打印(更改函数调用")定义主():根窗口 = Tk()rootWindow.geometry('400x400')rootWindow.configure(bg="red")全局标签label = ttk.Label( rootWindow, text="Hello World!" )标签.pack()button1 = ttk.Button( rootWindow, text="更改标签",命令=更改)button1.pack()rootWindow.mainloop()主要的()

解决方案

因此,在使用 tkinter 按钮与 ttk 样式按钮时,配置按钮颜色有点不同.

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

button1 = Button( rootWindow, text="更改标签",背景 = '黑色',前景 = 白色",命令 = 变化)

对于 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 配置的更多信息可以在

My task is to create a label and button on Tkinter. The button has to change the label, and I have to change the colour of the button and the label. I have changed the colour of the background but I can't figure out how to do the same for the label and button.

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()

解决方案

So configuring a buttons colors is a bit different when using tkinter button VS a ttk style button.

For a tkinter button you would use the background = "color" argument like the following:

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

For a ttk button you would configure the style and then use the style = "style name" argument like the following.

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

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

More information on ttk configs can be found 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()

Result:

这篇关于如何更改 Tkinter 上按钮和标签的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:43:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   如何更改   颜色   标签   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!