简体   繁体   English

如何使用 class ttk.Style 设置框架的背景?

[英]How to set the frame's background using class ttk.Style?

I'm making a tkinter interface and created a ttk.Style object to make it easy to change the design.我正在制作一个 tkinter 接口并创建了一个 ttk.Style object 以便于更改设计。 However, when I try to change some characteristic of the frame or labelframe, I don't see changes in the widget.但是,当我尝试更改框架或标签框架的某些特征时,我看不到小部件中的更改。

import tkinter as tk
from tkinter import ttk

colors = [
    '#203864',
    '#ADB9CA'
]

myfont = ('calibri', 18)

root = tk.Tk()

style = ttk.Style()
style.theme_create('MyTheme', parent='alt', settings={
    'TFrame':{
        'configure':{'background':colors[0]}
    },
    'TLabel':{
        'configure':{'background':colors[0], 'font':myfont, 'foreground':colors[1]}
    }
})
style.theme_use('MyTheme')

frame = tk.Frame(root)
frame.pack()

tk.Label(frame, text='MyTheme').pack(padx=100, pady=20)

root.mainloop()

Sorry for my English, I'm learning.对不起我的英语,我正在学习。

The themes only work for ttk widgets, and your code is using a tk frame.主题仅适用于 ttk 小部件,并且您的代码使用 tk 框架。 You need to change your frame to this:您需要将框架更改为:

frame = ttk.Frame(root, width=100, height=100)
#       ^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM