如何立即在Tkinter GUI中更改所有内容的颜色

如何立即在Tkinter GUI中更改所有内容的颜色,第1张

如何立即在Tkinter GUI中更改所有内容的颜色

您可以创建一个列表,其中包含要更改的所有小部件

myWidgets = [button1, label1, ... ] # List of widgets to change colourfor wid in myWidgets:    wid.configure(bg = newColour)

这是一次更改多个标签的背景色的示例代码。

import tkinter as tk# Change all label backgroundsdef change_colour():    c = user.get() #Get the entered text of the Entry widget    for wid in widget_list:        wid.configure(bg = c)# Create GUIroot = tk.Tk()tk.Label(root, text='Enter a colour').pack()user = tk.Entry(root)user.pack()label_frame = tk.frame(root)label_frame.pack()btn = tk.Button(root, text='Change Colour', command = change_colour)btn.pack()widget_list = [user, btn] # Add defined widgets to list#Dynamicly create labels for examplefor x in range(10):     lbl = tk.Label(label_frame, text='Label '+str(x))    lbl.pack(side = tk.LEFT)    widget_list.append(lbl) #Add widget object to listroot.mainloop()

或者,如果您的框架已经包含要更改的所有小部件,则可以改用它。

parent_widget.winfo_children()
将返回一个列表,其中包含所有存储在父窗口小部件内的窗口小部件

def change_colour():    c = user.get()    for wid in label_frame.winfo_children():        wid.configure(bg = c)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655169.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存