<div dir="ltr"><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 6, 2023 at 7:17 PM Florian Bruhin <<a href="mailto:me@the-compiler.org">me@the-compiler.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Hey,<br>
<br>
> You can simply create qapplication instance in your application<br>
> __init__.py, or make icon.py lazy, e.g.<br>
> <br>
> <span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"></span>from PyQt5.QtGui import QIcon<br>
> <br>
> ICONS = {'PAUSE': 'media-playback-pause'}<br>
> <br>
> def __getattr__(name):<br>
>     return globals().setdefault(name, QIcon.fromTheme(ICONS[name]))<br>
<br>
Personally, I'd probably do something like this, but a bit more<br>
explicit. I also like using enums for things like this, because then you<br>
get IDE autocompletion and such:<br>
<br>
    import enum<br>
    from PyQt5.QtGui import QIcon<br>
<br>
    class Icon(enum.Enum):<br>
<br>
        PAUSE = "media-playback-pause"<br>
        ...<br>
<br>
    def get(icon: Icon) -> QIcon:<br>
        return QIcon.fromTheme(icon.value)<br>
<br>
And then when you want an icon:<br>
<br>
    icon.get(icon.Icon.PAUSE)<br>
<br>
Florian<br></blockquote><div><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default">I think it would still be possible to get IDE autocompletion by adding __dir__ in the module</div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default">
<span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"></span>from PyQt5.QtGui import QIcon<br><br>ICONS = {'PAUSE': 'media-playback-pause'}<br><br>def __getattr__(name):<br>    return globals().setdefault(name, QIcon.fromTheme(ICONS[name])) <br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default">def __dir__():</div><div style="font-family:arial,helvetica,sans-serif;font-size:small" class="gmail_default">    return list(ICONS.keys())<br></div></div></div>