[英]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.