#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Tkinter how to set button bk color and style/exstyle?


    Hello.

    If I use:
    Code:
    button = ttk.Button(form, text="Generate", command=generate, bg="black")
    then I get this error:
    _tkinter.TclError: unknown option "-bg"

    Or if I use ttk.Style().configure("TButton", background="red") then it only changes border color.


    Edit: is there way to set button style in ttk?
    I want to change these styles http://msdn.microsoft.com/en-us/library/windows/desktop/bb775951%28v=vs.85%29.aspx
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    Check the Python manual under ttk
    The styling feature is very powerful, but also rather complex.
    Last edited by Dietrich; November 11th, 2012 at 09:40 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0
    That doesn't say how to do that. Or at least I couldn't find how.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    608
    Rep Power
    65
    Sorry, the manual tells

    background =
    Windows and Mac have a notion of an “active” or foreground window.
    The background state is set for widgets in a background window, and
    cleared for those in the foreground window
    Code:
    '''ttk_button_label1.py
    a look at foreground/background colors
    '''
    
    try:
        # Python27
        import Tkinter as tk
        import ttk
    except ImportError:
        # Python31+
        import tkinter as tk
        import tkinter.ttk as ttk
    
    root = tk.Tk()
    
    # typical tk button
    tk_btn = tk.Button(text="tk_Sample", bg='black', fg='red')
    tk_btn.pack(pady=10)
    
    # now a ttk button with styling (bg/fg won't do)
    ttk.Style().configure("RB.TButton", foreground='red', background='black')
    ttk_btn = ttk.Button(text="ttk_Sample", style="RB.TButton")
    ttk_btn.pack(pady=10)
    
    # foreground/background works with a ttk label but not a ttk button
    # the way you expect
    style = ttk.Style()
    style.configure("GB.TLabel", foreground="green", background="blue")
    ttk_label = ttk.Label(text="ttk_Label", style="GB.TLabel")
    ttk_label.pack(pady=10)
    
    root.mainloop()
    I am lost with this background thing. In desperation, you could put a label on the button instead of text. Have enough of the button show so it can be clicked.
    Last edited by Dietrich; November 11th, 2012 at 10:33 AM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0
    I tried ttk.Style().configure("TButton", relief="flat" ,background="red")


    but for some reason I only get border in red. other than that is still default color..... I am using windows..

IMN logo majestic logo threadwatch logo seochat tools logo