繁体   English   中英

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

[英]How to fill ttk.button with yellow in tkinter?

我使用 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'),  ['!active', 'yellow']],)

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

self.rule_button = ttk.Button(self.Canvas1, compound=tk.CENTER, 
                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'),  ['!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', font=('Helvetica', 25, 'bold'),text="4 person")

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

        self.styled_rule_button = ttk.Button(f, compound=tk.CENTER,  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()

在此处输入图片说明

暂无
暂无

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

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