-
Notifications
You must be signed in to change notification settings - Fork 114
Usage with Python
rdbende edited this page Nov 26, 2022
·
2 revisions
The sv_ttk
package allows you to easily apply the Sun Valley theme to your Tkinter/ttk app.
You can install it with pip
pip install sv-ttk
There are a two ways to set a theme.
You can use the use_dark_theme
and use_light_theme
to set dark or light theme.
import tkinter
from tkinter import ttk
import sv_ttk
root = tkinter.Tk()
button = ttk.Button(root, text="Button")
button.pack()
sv_ttk.use_dark_theme()
sv_ttk.use_light_theme()
root.mainloop()
With the set_theme
function, you can specify which theme you want to use. It accepts dark
or light
as a parameter and throws an error in any other case.
import tkinter
from tkinter import ttk
import sv_ttk
root = tkinter.Tk()
button = ttk.Button(root, text="Button")
button.pack()
sv_ttk.set_theme("dark")
root.mainloop()
You can toggle between dark and light themes using the toggle_theme
function. If the previous theme was not a Sun Valley theme, it will set to light theme.
import tkinter
from tkinter import ttk
import sv_ttk
root = tkinter.Tk()
button = ttk.Button(root, text="Toggle theme", command=sv_ttk.toggle_theme)
button.pack()
root.mainloop()
You can query the current theme with the get_theme
method. If the Sun Valley theme is in use, it will return either dark
or light
, otherwise it will return the theme's name.
import tkinter
from tkinter import ttk
import sv_ttk
root = tkinter.Tk()
def toggle_theme():
if sv_ttk.get_theme() == "dark":
print("Setting theme to light")
sv_ttk.use_light_theme()
elif sv_ttk.get_theme() == "light":
print("Setting theme to dark")
sv_ttk.use_dark_theme()
else:
print("Not Sun Valley theme")
button = ttk.Button(root, text="Toggle theme", command=toggle_theme)
button.pack()
root.mainloop()