鱼C论坛

 找回密码
 立即注册
查看: 1497|回复: 1

ttk的组件参数

[复制链接]
发表于 2021-2-4 21:19:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 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一改就报错,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-4 22:16:06 | 显示全部楼层


稀里糊涂弄出来了

给 configure 第一个参数 和 Button 的 style 参数进行对应即可:

  1. import tkinter as tk
  2. import tkinter.ttk as ttk


  3. class Button_ttk:
  4.     def __init__(self, root):
  5.         self.root = root

  6.         ttk.Style().configure('First.TButton', relief="flat", foreground='red')

  7.         btn = ttk.Button(self.root, text='xxx', style='First.TButton', command=self.fun_with_args(0))
  8.         btn.grid()

  9.         ttk.Style().configure('Second.TButton', relief="sunken", foreground='blue')

  10.         btn1 = ttk.Button(self.root, text='xxx', style='Second.TButton', command=self.fun_with_args(0))
  11.         btn1.grid()

  12.     def fun_with_args(self, x):
  13.         print('------------------------')


  14. if __name__ == '__main__':
  15.     root = tk.Tk()
  16.     Button_ttk(root)

  17.     tk.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-4 12:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表