ttk的组件参数
本帖最后由 lawrence1357 于 2021-2-4 21:35 编辑import tkinter as tk
import tkinter.ttk as ttk
class Button_ttk:
def __init__(self, root):
self.root = root
ttk.Style().configure('TButton', relief="flat", foreground='red')
btn = ttk.Button(self.root,text='xxx',style='TButton',command=self.fun_with_args(0))
btn.grid()
ttk.Style().configure('TButton', relief="sunken", foreground='blue')
btn1 = ttk.Button(self.root, text='xxx', style='TButton', command=self.fun_with_args(0))
btn1.grid()
def fun_with_args(self,x):
print('------------------------')
if __name__=='__main__':
root=tk.Tk()
Button_ttk(root)
tk.mainloop()
如果我想第一个Button的foreground是红色,第二个Button的foreground是蓝色,这时候怎么设置呢,这个TButton一改就报错,谢谢
稀里糊涂弄出来了
给 configure 第一个参数 和 Button 的 style 参数进行对应即可:
import tkinter as tk
import tkinter.ttk as ttk
class Button_ttk:
def __init__(self, root):
self.root = root
ttk.Style().configure('First.TButton', relief="flat", foreground='red')
btn = ttk.Button(self.root, text='xxx', style='First.TButton', command=self.fun_with_args(0))
btn.grid()
ttk.Style().configure('Second.TButton', relief="sunken", foreground='blue')
btn1 = ttk.Button(self.root, text='xxx', style='Second.TButton', command=self.fun_with_args(0))
btn1.grid()
def fun_with_args(self, x):
print('------------------------')
if __name__ == '__main__':
root = tk.Tk()
Button_ttk(root)
tk.mainloop()
页:
[1]