View Single Post
Old 12-07-2024, 06:52 PM   #1886
dunhill
Guru
dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.dunhill ought to be getting tired of karma fortunes by now.
 
dunhill's Avatar
 
Posts: 893
Karma: 810834
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
Quote:
Originally Posted by thiago.eec View Post
Did you tried what I suggested via PM? Here is with a detailed explanation (Python 3):

First, put your suffixed icons in the 'images' folder inside your plugin zip file. E.g.: 'images/icon_dark.png', 'images/icon_light.png'.

Place this at the start of your main.py or similar:
Code:
# Check for dark theme
def is_dark_theme():
    return QApplication.instance().is_dark_theme
Then, the get_icon() function:
Code:
def get_icon(icon_name):
    icon_name = 'images/' + icon_name

    # Choose the suffix
    tc = 'dark' if is_dark_theme() else 'light'
    sq, ext = os.path.splitext(icon_name)
    sq = f'{sq}_{tc}{ext}'

    return get_icons(sq)
Then, in your code, use the get_icon() function to retrieve the icon like this:

Code:
icon = get_icon(icon_name.png)
Note that you use the icon name without the suffix (dark/light).

This will allow the plugin to change from light/dark theme when calibre starts. If you want it to change without restarting, than you need to rebuild the menus on the fly. Check out the rebuild_menus() method present in many plugins (ui.py).
Now, thank you very much.
dunhill is offline   Reply With Quote