【问题标题】:How to fill ttk.button with yellow in tkinter?如何在 tkinter 中用黄色填充 ttk.button?
【发布时间】:2020-11-11 01:58:39
【问题描述】:

我使用 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时如何配置?

【问题讨论】:

  • 您无法使用默认主题更改按钮背景。尝试使用其他主题。
  • 尝试其他一些主题,如 clam、alt、winative 等,

标签: python tkinter tkinter-canvas tkinter-layout


【解决方案1】:

对我来说,按钮看起来一样,但也许我错过了什么。

我做了一个例子,我在 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()

【讨论】:

    猜你喜欢
    • 2019-10-03
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2014-12-13
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多