如何在tkinter中用黄色填充ttk.button?

问题描述

我使用tk.Button制作了如下所示的按钮

self.rule_button = tk.Button(self.Canvas1,compound=tk.CENTER,bg= 'yellow',font=('Helvetica',25,'bold'),text="4 person")
self.button1_window = self.Canvas1.create_window(self.screenwidth/5 * 4,self.screenheight/5 * 3,window=self.rule_button)

我使用ttk.Button制作了如下所示的按钮

self.style = ttk.Style()

self.style.map("C.TButton",foreground=[('pressed','red'),('active','blue')],background=[('pressed','!disabled','yellow'),['!active','yellow']],)

self.style.configure("C.TButton",width = 6,padding = 10)

self.rule_button = ttk.Button(self.Canvas1,text="4 person",style="C.TButton")  

          

但是按钮没有像tk.button中那样用黄色填充。

使用ttk时如何配置?

解决方法

对我来说,这些按钮看起来相同,但也许我错过了一些事情。

我已经举了一个例子,我在Debian上。

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW",self.on_exit)
        #self.geometry("400x300")
        self.title("Simple App")

        self.style = ttk.Style()

        #...but...the bg colour it's ever yellow?
        self.style.map("C.TButton",foreground=[('pressed','red'),('active','blue')],background=[('pressed','!disabled','yellow'),['!active','yellow']],)

        self.style.configure("C.TButton",width = 6,font=('Helvetica',25,'bold'),padding = 10)

        
        self.init_ui()
       
    def init_ui(self):

        f = ttk.Frame(padding = 8)

        self.rule_button = tk.Button(f,compound=tk.CENTER,bg= 'yellow',text="4 person")

        self.rule_button.pack(fill=tk.X,expand=1)

        self.styled_rule_button = ttk.Button(f,text="4 person",style="C.TButton")
        self.styled_rule_button.pack(fill=tk.X,expand=1)
        
        f.pack(fill=tk.BOTH,expand=1)        
            
    def on_exit(self):
        if messagebox.askokcancel(self.title(),"Do you want to quit?",parent=self):
            self.destroy()               
    
if __name__ == '__main__':
    app = App()
    app.mainloop()

enter image description here