Search code examples
python-3.xtkinterstylesttk

Why do two windows appear when I connect styles?


enter image description hereI have a code like this:

from tkinter import Tk
import tkinter.ttk as ttk

"""Styles"""
style = ttk.Style()
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
          foreground=[('pressed', 'white'), ('active', 'white')],
          background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])

root = Tk()

button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)

root.mainloop()

I'm new at this. I searched the Internet for a solution, but could not find it. Everywhere they write about widthdraw(), but this does not help. Two windows always appear and the customized style is not applied to the button. What am I doing wrong? How do I search Google for this problem? Tell me please. Thanks.


Solution

  • You just need to define ttk.Style() inside the root window.

    from tkinter import Tk
    import tkinter.ttk as ttk
    
    root = Tk()
    
    """Styles"""
    style = ttk.Style()
    
    # add the these_use option and use the 'clam' theme
    style.theme_use('clam')
    
    style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
    style.map('OrangeButton.TButton',
              foreground=[('pressed', 'white'), ('active', 'white')],
              background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])
    
    button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
    button.pack(padx=50, pady=50)
    
    root.mainloop()
    

    Hope this solves the problem.