问题描述
限时送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:关键词]
发布评论