Search code examples
pythontkinterttk

How to install awdark theme into ttk python


I want to have the awdark ttk theme for my app, but I can't figure out how to install it. I found source files here https://sourceforge.net/projects/tcl-awthemes/ but I don't know where to put any of the files. Any help would be much appreciated. I'm on windows 10, python3.8.3 64bit.


Solution

  • You can load the themes by executing a few tcl commands:

    1. Tell tcl where to find awthemes

      root.tk.eval("""
          set base_theme_dir /path/to/downloaded/theme/awthemes-9.2.2/
      
          package ifneeded awthemes 9.2.2 \
              [list source [file join $base_theme_dir awthemes.tcl]]
          package ifneeded colorutils 4.8 \
              [list source [file join $base_theme_dir colorutils.tcl]]
          package ifneeded awdark 7.7 \
              [list source [file join $base_theme_dir awdark.tcl]]
          # ... (you can add the other themes from the package if you want
          """)
      
    2. Load the awdark theme: root.tk.call("package", "require", 'awdark')

    3. Change the theme the usual way: style.theme_use('awdark')

    Here is a full example:

    from tkinter import ttk
    import tkinter as tk
    
    root = tk.Tk()
    style = ttk.Style(root)
    
    # tell tcl where to find the awthemes packages
    root.tk.eval("""
    set base_theme_dir /path/to/downloaded/theme/awthemes-9.2.2/
    
    package ifneeded awthemes 9.2.2 \
        [list source [file join $base_theme_dir awthemes.tcl]]
    package ifneeded colorutils 4.8 \
        [list source [file join $base_theme_dir colorutils.tcl]]
    package ifneeded awdark 7.7 \
        [list source [file join $base_theme_dir awdark.tcl]]
    package ifneeded awlight 7.6 \
        [list source [file join $base_theme_dir awlight.tcl]]
    """)
    # load the awdark and awlight themes
    root.tk.call("package", "require", 'awdark')
    root.tk.call("package", "require", 'awlight')
    
    print(style.theme_names())
    # --> ('awlight', 'clam', 'alt', 'default', 'awdark', 'classic')
    
    style.theme_use('awdark')
    
    ttk.Button(root, text='Button').pack()
    ttk.Checkbutton(root, text='Check Button').pack()
    ttk.Radiobutton(root, text='Radio Button').pack()
    root.configure(bg=style.lookup('TFrame', 'background'))
    root.mainloop()
    

    screenshot