Python Tkinter 调整所有 ttkbootstrap 或 ttk 按钮填充的大小以实现特定样式,应用于所有主题

问题描述 投票:0回答:1

我想使用特定样式更改所有按钮的填充(危险)。由于某种原因,此更改仅适用于当前活动的主题,切换主题会将按钮填充恢复为默认值。您可以通过运行以下命令并切换主题来查看问题...

import tkinter as tk
from tkinter import ttk
import ttkbootstrap as tb

def change_theme(theme, style): style.theme_use(theme.lower().replace(" ", ""))

def display_text(label_text, entry_text): label_text.set(entry_text.get())

def setup_ui(style):
    root = style.master

    danger = tb.Style()
    danger.configure('danger.TButton', padding=0) # Why does this only apply to the first theme?

    theme_names_titlecase = [name.replace('_', ' ').title() for name in style.theme_names() if name.lower() in ['darkly', 'simplex']]
    default_theme = 'darkly'
    current_theme = tk.StringVar(value=default_theme.capitalize())
    
    theme_combo = ttk.Combobox(root, textvariable=current_theme, values=theme_names_titlecase, width=50)
    theme_combo.pack(pady=0, side=tk.TOP)
    theme_combo.bind("<<ComboboxSelected>>", lambda e: change_theme(current_theme.get(), style))

    tb.Button(root, text='Text', bootstyle='danger.TButton').pack(side=tk.TOP, padx=0, pady=0)
    tb.Button(root, text='Text', bootstyle='info.TButton').pack(side=tk.TOP, padx=0, pady=0)

    return root

if __name__ == "__main__":
    default_theme = 'darkly'
    style = tb.Style(theme=default_theme)
    root = setup_ui(style)
    root.mainloop()

我想知道的是:

  1. 为什么我对“danger.TButton”的更改仅应用于当前主题?
  2. 我可以解决这个问题,让所有 'danger.TButton' 都没有填充,无论主题如何吗?

注意:使用所有 ttk 小部件和样式具有相同的结果,因此答案与 ttk 相关,而不是特别与 ttkbootstrap 相关。

非常感谢。

python tkinter ttk ttkbootstrap
1个回答
0
投票

主要原因是这部分以及在

tb.Style()
内配置
setup_ui()

danger = tb.Style()
danger.configure('danger.TButton', padding=0) # Why does this only apply to the first theme?

通过直接在

danger.TButton
的主实例上配置
tb.Style()
,您的自定义配置将一致地应用于所有主题。 这是您的代码的解决方案:

import tkinter as tk
from tkinter import ttk
import ttkbootstrap as tb

def change_theme(theme, style):style.theme_use(theme.lower().replace(" ", ""))

def display_text(label_text, entry_text):label_text.set(entry_text.get())

def setup_ui(style):
    root = style.master

    theme_names_titlecase = [name.replace('_', ' ').title() for name in style.theme_names() if name.lower() in ['darkly', 'simplex']]
    default_theme = 'darkly'
    current_theme = tk.StringVar(value=default_theme.capitalize())
    
    theme_combo = ttk.Combobox(root, textvariable=current_theme, values=theme_names_titlecase, width=50)
    theme_combo.pack(pady=0, side=tk.TOP)
    theme_combo.bind("<<ComboboxSelected>>", lambda e: change_theme(current_theme.get(), style))

    tb.Button(root, text='Text', bootstyle='danger.TButton').pack(side=tk.TOP, padx=0, pady=0)
    tb.Button(root, text='Text', bootstyle='info.TButton').pack(side=tk.TOP, padx=0, pady=0)

    return root

if __name__ == "__main__":
    default_theme = 'darkly'
    style = tb.Style(theme=default_theme)

    style.configure('danger.TButton', padding=0)

    root = setup_ui(style)
    root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.