简体   繁体   中英

How to switch between dark and light ttk theme?

How do I switch between ttk themes? I tried this:

style = ttk.Style(root)
root.tk.call('source', r'Azure-ttk-theme\azure dark\azure_dark.tcl')
root.tk.call('source', r'Azure-ttk-theme\azure\azure.tcl')
style.theme_use('azure')

I created a style, then when a button is pressed, this function is executed:

def change_theme():
    if style.theme_use() == 'azure':
        style.theme_use('azure_dark')
    else:
        style.theme_use('azure')

But this doesn't work as expected, the background color of the window does not change:

With light theme:

轻主题

After changing to dark theme:

只有按钮更改

It does not change properly.

But it's supposed to look like this after changing theme: 黑暗主题

Here's a small example with default styles:

from tkinter import ttk
import tkinter

def change_theme():
    if style.theme_use() == 'alt':
        style.theme_use('clam')
        root.configure(background='grey')
    else:
        style.theme_use('alt')
        root.configure(background='white')

root = tkinter.Tk()
#root.tk.call('source', r'Azure-ttk-theme\azure dark\azure_dark.tcl')
#root.tk.call('source', r'Azure-ttk-theme\azure\azure.tcl')

style = ttk.Style(root)
style.theme_use('alt')

frame = ttk.Frame(root).grid()

btn = ttk.Button(frame, text="Sample", command=lambda: change_theme())
btn.grid(column=0, row=1)

root.mainloop()

Uncomment those style fetches and change 'alt' and 'clam' to 'azure' and 'azure_dark' and it should work.

Try root.tk_setPalette( "#555555" ) or any color you like.

Themes will not change color but will just change widget styles, especially Scrollbars .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM