我想允許用戶配置應用程式的外觀(背景顏色、字體、字體大小和顏色、按鈕顏色等)。我已將所有默認設定保存到組態檔中,程式對其進行解釋并將其保存到類中的變數中,以便在函式之間進行訪問。現在,我可以將用戶所做的更改保存到組態檔中,這些更改將在用戶關閉并重新打開應用程式時反映出來,但我希望更改是即時的,所以我嘗試了這樣的操作:
import tkinter as tk
class SetColor:
def __init__(self, color):
self.color = 'green'
current = SetColor('green')
root = tk.Tk()
lbl_color = tk.Label(root, text='Choose button color')
lbl_color.grid(row=0, column=0, pady=5, padx=2)
btn_red = tk.Button(root, text='Red', bg=current.color, command=lambda:update_color('red'))
btn_red.grid(row=0, column=1, pady=5, padx=2)
btn_green = tk.Button(root, text='Green', bg=current.color, command=lambda:update_color('green'))
btn_green.grid(row=0, column=2, pady=5, padx=2)
btn_blue = tk.Button(root, text='Blue', bg=current.color, command=lambda:update_color('blue'))
btn_blue.grid(row=0, column=3, pady=5, padx=2)
def update_color(color):
current.color = color
#THIS is where I'm wondering if there's a way to refresh without individually updating each widget as I've done below
btn_red.config(bg=current.color)
btn_green.config(bg=current.color)
btn_blue.config(bg=current.color)
root.mainloop()
這確實有效,但在我的實際應用程式中,需要更新的小部件比這個^示例中的要多得多。所以我有一種感覺,我錯過了一些東西,或者以錯誤的方式處理這個問題。非常感謝任何幫助:)
uj5u.com熱心網友回復:
最好的辦法是將按鈕存盤在一個串列中并回圈遍歷該串列。這樣你就可以分開不同的按鈕。但是,如果您確定要更改每個單個按鈕的顏色,您可以這樣做:for widget in root.winfo_children():
if isinstance(widget, tk.Button):
widget.config(bg=current.color)
uj5u.com熱心網友回復:
@Maarten 的答案非常適合 tkinter 按鈕。在這種情況下可以使用另一個使用 ttk.Button 的選項
使用自定義樣式創建按鈕物件
btn_green = ttk.Button(root, text='Green', style="color.TButton", command=lambda: update_color('green'))
創建樣式物件
style = ttk.Style()
style.theme_use("default")
設定樣式
style.configure('color.TButton', background=current.color)
# Activate is when you mouse over the button.
style.map('color.TButton', background=[('active', current.color)])
完整示例:
import tkinter as tk
from tkinter import ttk
class SetColor:
def __init__(self, color):
self.color = 'green'
def update_color(color):
current.color = color
# Let's set the style
# naming that style variable as color.TButton
# NOTE: .TButton is important, you can add any other pretix though
style.configure('color.TButton', background=current.color)
# Activate is when you mouse over the button.
style.map('color.TButton', background=[('active', current.color)])
current = SetColor('green')
root = tk.Tk()
# Create style Object
style = ttk.Style()
# Setting theme to default (built in themes can be found https://wiki.tcl-lang.org/page/List of ttk Themes)
style.theme_use("default")
lbl_color = ttk.Label(root, text='Choose button color')
lbl_color.grid(row=0, column=0, pady=5, padx=2)
btn_red = ttk.Button(root, text='Red', style="color.TButton", command=lambda: update_color('red'))
btn_red.grid(row=0, column=1, pady=5, padx=2)
btn_green = ttk.Button(root, text='Green', style="color.TButton", command=lambda: update_color('green'))
btn_green.grid(row=0, column=2, pady=5, padx=2)
btn_blue = ttk.Button(root, text='Blue', style="color.TButton", command=lambda: update_color('blue'))
btn_blue.grid(row=0, column=3, pady=5, padx=2)
update_color(current.color)
root.mainloop()
ttk 風格還有很多選擇。
看一下
Python ttk 風格
ttk 主題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419077.html
標籤:
下一篇:泛型方法引數<T>檢測為變數
