如何改变一切颜色在Tkinter的GUI一次 [英] How to change the colour of everything in a tkinter GUI at once

查看:508
本文介绍了如何改变一切颜色在Tkinter的GUI一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code(如下所示),其提示用户选择的颜色来改变GUI来。但我的问题是,它只是改变了背景。我想知道,如果有改变每个标签和按钮的背景一次的方式或做我必须分别更改每个标签/按钮。

 导入Tkinter的
窗口= tkinter.Tk()
colour_frame = tkinter.Frame(窗口)
options_frame = tkinter.Frame(窗口)DEF颜色():
    options_frame.pack_forget()
    red.pack()
    orange.pack()
    back_button.pack()
    colour_frame.pack()高清返回():
    options_frame.pack()
    colour_frame.pack_forget()高清make_red():
    window.configure(背景=红)高清make_orange():
    window.configure(背景=橙色)colour_button = tkinter.Button(options_frame,文本=外观,命令=颜色)红色= tkinter.Button(colour_frame,文本=RED,命令= make_red)
red.configure(BG =红)
橙色= tkinter.Button(colour_frame,文本=ORANGE,命令= make_orange)
orange.configure(BG =橙色)
back_button = tkinter.Button(colour_frame,文本=返回,命令=回)window.mainloop()


解决方案

您可以创建包含你想改变你所有的部件清单

  myWidgets = [BUTTON1,LABEL1,...]小部件#列表改变颜色
对于妇女参与发展的myWidgets:
    wid.configure(BG = newColour)

下面是一次改变多个标签的背景颜色的例子code。

 导入Tkinter的传统知识
#更改所有标签背景
高清change_colour():
    C = user.get()#获取的输入构件输入的文本
    对于妇女参与发展的widget_list:
        wid.configure(BG = C)#创建GUI
根= tk.Tk()tk.Label(根,文本=输入颜色)。包()用户= tk.Entry(根)
user.pack()label_frame = tk.Frame(根)
label_frame.pack()BTN = tk.Button(根,文本='更改颜色',命令= change_colour)
btn.pack()widget_list = [用户,BTN]#添加小部件定义要列出#Dynamicly创建例如标签
对于在范围X(10):
    LBL = tk.Label(label_frame,文本=标签+ STR(X))
    lbl.pack(侧= tk.LEFT)
    widget_list.append(LBL)#将widget对象为列表root.mainloop()

或者,如果你已经包含了所有你想要更改的小部件的框架,那么你可以用这个来代替。

parent_widget.winfo_children()将返回包含存储在父部件内的所有控件列表

  DEF change_colour():
    C = user.get()
    对于妇女参与发展的label_frame.winfo_children():
        wid.configure(BG = C)

I have some code (as shown below) which prompts the user to select which colour to change the GUI to. But my problem is that it only changes the background. I'd like to know if there's a way to change the background of every label and button at once or do I have to change each label/button individually.

import tkinter
window = tkinter.Tk()  
colour_frame = tkinter.Frame(window)
options_frame = tkinter.Frame(window)

def colours():
    options_frame.pack_forget()
    red.pack()
    orange.pack()
    back_button.pack()
    colour_frame.pack()

def back():
    options_frame.pack()
    colour_frame.pack_forget()

def make_red():
    window.configure(background="red")

def make_orange():
    window.configure(background="orange")

colour_button = tkinter.Button(options_frame, text="Appearance", command=colours)

red = tkinter.Button(colour_frame, text="RED", command=make_red)
red.configure(bg = "red")
orange = tkinter.Button(colour_frame, text="ORANGE", command=make_orange)
orange.configure(bg = "orange")
back_button = tkinter.Button(colour_frame, text="Back", command=back)

window.mainloop()

解决方案

You can make a list containing all your widgets you want to change

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

Here's an example code of changing the background colour of multiple labels at once.

import tkinter as tk


# Change all label backgrounds
def change_colour():
    c = user.get() #Get the entered text of the Entry widget
    for wid in widget_list:
        wid.configure(bg = c)

# Create GUI
root = 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 example
for 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 list

root.mainloop()

Or if you have a Frame already containing all the widgets you want to change, then you can use this instead.

parent_widget.winfo_children() will return a list containing all the widgets stored inside the parent widget

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

这篇关于如何改变一切颜色在Tkinter的GUI一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆