How to change the color of ttk button

前端 未结 3 1860
长发绾君心
长发绾君心 2020-12-31 05:37

I am using Python 3.x on Windows.

My problem is I want to customize a button widget of ttk by completely changing its background and foreground color.

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 06:33

    Although it is not as simple as with Tk buttons, it is possible. In ttk, if you set the theme_use attribute to any of these: ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'), you should be able to modify the default behaviour. I set the "style.map" attribute to avoid background colour change due to mouse hover (The state of the button is always 'active').

    import tkinter as tk
    from tkinter import ttk 
    
    style = ttk.Style()
    style.theme_use('alt')
    style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none')
    style.map('TButton', background=[('active','red')])
    
    root = tk.Tk()
    button = ttk.Button(root,text='Quit')
    button.place(relx=0.3,rely=0.4)  
    root.mainloop()      
    

    Hope this helps.

提交回复
热议问题