【问题标题】:How to change the colour of everything in a tkinter GUI at once如何一次更改 tkinter GUI 中所有内容的颜色
【发布时间】:2016-01-24 04:35:06
【问题描述】:

我有一些代码(如下所示)提示用户选择将 GUI 更改为哪种颜色。但我的问题是它只会改变背景。我想知道是否有办法一次更改每个标签和按钮的背景,还是我必须单独更改每个标签/按钮。

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

【问题讨论】:

    标签: python user-interface button background tkinter


    【解决方案1】:

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

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

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

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

    或者,如果您的 Frame 已经包含您想要更改的所有小部件,那么您可以使用它来代替。

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

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

    【讨论】:

      【解决方案2】:

      尝试对您的一些 GUI 元素使用 ttk。 ttk 允许您为小部件创建样式并一次将样式更新到所有小部件(至少对于那些具有相同样式的小部件)。您可能需要混合使用 ttk 和 tkinter,但从长远来看,它应该会让事情变得更容易一些。这是我做的一个例子:

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

      我建议查看this site 以了解有关 ttk 和样式的更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 2017-04-23
        • 2018-06-10
        • 1970-01-01
        • 2020-03-09
        • 2015-04-17
        相关资源
        最近更新 更多