How to install awdark theme into ttk python

末鹿安然 提交于 2020-07-19 06:45:47

问题


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.


回答1:


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()



来源:https://stackoverflow.com/questions/62927068/how-to-install-awdark-theme-into-ttk-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!