python 为什么一个自定义ttk.style()的函数不起作用?

htrmnn0y  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(225)

我不知道为什么customise_ttk_widgets_style(ss)函数导致ttk.Buttonttk.Labels不能正确显示。注解掉# s = customise_ttk_style(s)行可以避免错误的小部件显示,但我不明白为什么会这样。你能解释一下问题的原因和解决方法吗?谢谢。

import tkinter as tk
import tkinter.ttk as ttk

BG = '#3b3b39'
FG = 'white'
DFONT = ('URW Gothic L', '10', 'Normal')

def customise_ttk_widgets_style(ss):
    # All Widgets
    ss.configure(".", font=DFONT, background=BG, foreground=FG, cap=tk.ROUND,
                 join=tk.ROUND)
    
    # main ttk.Frame
    ss.configure('Main.TFrame', background='pink')  # For debugging

    # Default ttk.Button
    ss.configure('TButton', padding=5, relief=tk.FLAT)
    ss.map('TButton',
           foreground=[("disabled", 'grey'), ('pressed', 'red'),
                       ('active', 'yellow')],
           background=[('disabled', '#646a67'), ('pressed', '!focus', BG),
                       ('active', '#535553')],
           relief=[('pressed', 'sunken'), ('!pressed', 'raised')],
           )
    return ss

if __name__ == "__main__":
    root = tk.Tk()
    root['background'] = BG
    s = ttk.Style()
    s = customise_ttk_widgets_style(s)  # This function is causing problem
    button = ttk.Button(root, text="ttk.Button (!disabled)")
    dbutton = ttk.Button(root, text="ttk.Button (disabled)")
    dbutton.state(['!disabled', 'disabled'])
    label = ttk.Label(root, text="ttk.Label (!disabled)")
    dlabel = ttk.Label(root, text="ttk.Label (disabled)")
    dlabel.state(['!disabled', 'disabled'])
    button.grid(row=0, column=0)
    dbutton.grid(row=0, column=1)
    label.grid(row=1, column=0)
    dlabel.grid(row=1, column=1)
    root.mainloop()

正确外观:

外观错误:第一节第一节第一节第二节第一节

1mrurvl1

1mrurvl11#

终于解决了问题,DFONT的定义是错误的,应该定义为:

from tkinter import font
DFONT = font.Font(family='URW Gothic L', size=10, weight='bold')

但是这个定义不能是全局变量,因为需要先定义tk.Tk(),如果DFONT是全局变量,则应该定义为dict()对象,即

DFONT = dict(family='URW Gothic L', size=10, weight='bold')

此后,在customise_ttk_widgets_style()函数中,将

ss.configure(".", font=DFONT, background=BG, foreground=FG,
                 cap=tk.ROUND, join=tk.ROUND)

正确语法为:

ss.configure(".", font=font.Font(**DFONT), background=BG, foreground=FG,
                 cap=tk.ROUND, join=tk.ROUND)

更正后的脚本:

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import font

BG = '#3b3b39'
FG = 'white'
DFONT = dict(family='URW Gothic L', size=10, weight='bold')

def customise_ttk_widgets_style(ss):
    # All Widgets
    ss.configure(".", font=font.Font(**DFONT), background=BG, foreground=FG,
                 cap=tk.ROUND, join=tk.ROUND)
    # main ttk.Frame
    ss.configure('Main.TFrame', background='pink')  # For debugging

    # Default ttk.Button
    ss.configure('TButton', padding=5, relief=tk.FLAT)
    ss.map('TButton',
           foreground=[("disabled", 'grey'), ('pressed', 'red'),
                       ('active', 'yellow')],
           background=[('disabled', '#646a67'), ('pressed', '!focus', BG),
                       ('active', '#535553')],
           relief=[('pressed', 'sunken'), ('!pressed', 'raised')],
           )
    return ss

if __name__ == "__main__":
    root = tk.Tk()
    root['background'] = BG
    s = ttk.Style()
    s = customise_ttk_widgets_style(s)
    button = ttk.Button(root, text="ttk.Button (!disabled)")
    dbutton = ttk.Button(root, text="ttk.Button (disabled)")
    dbutton.state(['!disabled', 'disabled'])
    label = ttk.Label(root, text="ttk.Label (!disabled)")
    dlabel = ttk.Label(root, text="ttk.Label (disabled)")
    dlabel.state(['!disabled', 'disabled'])
    button.grid(row=0, column=0)
    dbutton.grid(row=0, column=1)
    label.grid(row=1, column=0)
    dlabel.grid(row=1, column=1)
    root.mainloop()

相关问题