python - 如何在 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'), ['!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()
推荐阅读
- html - 绝对定位元素在使用过滤器的活动时丢失位置
- typescript - Typescript 泛型类型和多态性
- react-native - 如何使用 React Navigation v5 使用嵌套导航保持初始路线?
- python - 如果与主要刻度一致,则不会出现 Matplotlib 次要刻度
- python - 尝试抓取的 URL 会加载不同的页面
- reactjs - 动态(和全局)设置在 react-native 中聚焦的输入字段
- python - 如何合并和计算两个数据框?
- laravel - Laravel-vuejs(Vue 路由器)Javascript 在 onchange url 中不起作用
- java - setLightLevel 属性无法正常工作
- angular - Angular Event Binder Error - 作为值传递时将数字转换为字符串的函数参数