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

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. 我能解决这个问题吗?不管主题如何,TButton都没有填充?

注意:使用所有ttk小部件和Styles会有相同的结果,所以答案与ttk无关,特别是ttkbootstrap.

多谢了.

推荐答案

主要原因是这部分和配置tb.Style()内的setup_ui().这意味着你为danger.TButton设置的配置只与tb.Style()的实例相关联,所以,当你试图改变你的主题时,ttkbootstrap会在内部创建一个新的实例tb.Style(),而你的自定义配置不会延续到这个新实例.

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

通过在tb.Style()的主实例上直接配置danger.TButton,您的自定义配置将在所有主题中一致地apply. 以下是您代码的解决方案:

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) # Why does this only apply to the first theme?


    root = setup_ui(style)
    root.mainloop()

Python相关问答推荐

根据网格和相机参数渲染深度

将numpy数组存储在原始二进制文件中

对Numpy函数进行载体化

如何使用symy打印方程?

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

Mistral模型为不同的输入文本生成相同的嵌入

如何获取numpy数组的特定索引值?

如何使用pytest来查看Python中是否存在class attribution属性?

给定高度约束的旋转角解析求解

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

在两极中过滤

python panda ExcelWriter切换动态公式到数组公式

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

为什么我的sundaram筛这么低效

如果包含特定值,则筛选Groupby

数据框,如果值在范围内,则获取范围和

高效生成累积式三角矩阵

为什么Visual Studio Code说我的代码在使用Pandas concat函数后无法访问?

EST格式的Azure数据库笔记本中的当前时间戳