How to change the colour of everything in a tkinter GUI at once

后端 未结 2 1536
别跟我提以往
别跟我提以往 2020-12-11 22:39

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 kno

相关标签:
2条回答
  • 2020-12-11 23:16

    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)
    
    0 讨论(0)
  • 2020-12-11 23:23

    Try using ttk for some of your GUI elements. ttk allows you to create styles for widgets and update the style to all widgets at once (at least for those that have the same style). You may need to mix the usage of ttk and tkinter, but it should make things a bit easier in the long run. Here is an example I made:

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    
    # Creating a style for the buttons
    color_style_button = ttk.Style()
    color_style_button.configure("color.TButton", foreground="red")
    
    def change_color(color):
        # This function changes the style to all buttons using the "color.Button style"
    
        if color == "red":
            color_style_button.configure("color.TButton", foreground="red")
        elif color == "blue":
            color_style_button.configure("color.TButton", foreground="blue")
        elif color == "green":
            color_style_button.configure("color.TButton", foreground="green")
    
    frame_a = ttk.Frame(root)
    frame_a.pack()
    
    red_button = ttk.Button(frame_a, text="Red", command=lambda: change_color("red"), style="color.TButton")
    red_button.pack()
    
    blue_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("blue"), style="color.TButton")
    blue_button.pack()
    
    green_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("green"), style="color.TButton")
    green_button.pack()
    
    root.mainloop()
    

    I recommend checking out this site to learn more about ttk and styles.

    0 讨论(0)
提交回复
热议问题