Search code examples
pythonpython-3.xtkinterpyinstallereasygui

Python EasyGUI Changing Taskbar Icon


I've been using easyGUI to build a GUI for my python script. I'm having trouble finding a way to change the taskbar icon.

I am also using pyinstaller to convert the python file to a exe and was able to change the exe's icon, but when I run the file the easyGUI blue feather appears in the taskbar. I've seen how you can change it in Tkinter, I know that easyGUI uses Tkinter, but how can I miniplate the taskbar icon while using easyGUI?


Solution

  • Icon customization is not supported currently with easyGUI. However, you can add support for this relatively easily.


    Let's take a look at a random easyGUI object: ChoiceBox. The source code for this very simple. (Find it in your Lib\site-packages\easygui\boxes folder.) You can follow the same routine for any type of easyGUI window you're using. A bit lengthy, but it works.

    def choicebox(msg="Pick an item", title="", choices=None, preselect=0,
                  callback=None,
                  run=True):
        """
        The ``choicebox()`` provides a list of choices in a list box to choose
        from. The choices are specified in a sequence (a tuple or a list).
    
        :param str msg: the msg to be displayed
        :param str title: the window title
        :param list choices: a list or tuple of the choices to be displayed
        :param preselect: Which item, if any are preselected when dialog appears
        :return: A string of the selected choice or None if cancelled
        """
    
        mb = ChoiceBox(msg, title, choices, preselect=preselect,
                       multiple_select=False,
                       callback=callback)
    
        if run:
            reply = mb.run()
            return reply
        else:
            return mb
    

    Let's add a new parameter to this, called icon.

    def choicebox(msg="Pick an item", title="", choices=None, preselect=0,
                  callback=None,
                  run=True, icon=None):
    

    We need to add it to the underlying class as well, ChoiceBox.

    mb = ChoiceBox(msg, title, choices, preselect=preselect,
                       multiple_select=False,
                       callback=callback, icon=icon)
    

    Next, in the ChoiceBox class, we do the same thing. Note how I added icon to the GUItk object's parameters.

    class ChoiceBox(object):
    
        def __init__(self, msg, title, choices, preselect, multiple_select, callback, icon):
    
            ...
    
            self.ui = GUItk(msg, title, self.choices, preselect_list, multiple_select,
                            self.callback_ui, icon)
    

    Here's the GUItk __init__ code. Add the icon parameter to this as well. We'll also finally call the actual method that will do the changing of icons for us.

    def __init__(self, msg, title, choices, preselect, multiple_select, callback, icon):
    
            ...
    
            self.config_root(title) #No need to add this line, just for location reference.
            self.config_icon(icon) #Add this line
    

    And finally, the config_icon method, which you should also put right below the config_root method.

    def config_icon(self, icon):
            if icon:
                self.boxRoot.iconbitmap(icon)
    

    Add it to your code like this: choicebox(choices=['choice1', 'choice2'], icon="pathto\icon.ico")

    And then if you compile this version of easyGUI into your pyinstaller, it should appear. (Remember the value of icon should be a string of a path pointing to an .ico file. Also, note this has no error handling built in. If the path to your icon is wrong, then it won't catch that.) If you're trying to run it without pyinstaller, view this answer.

    Hope this was helpful! I know it's tedious, but it stays relatively consistent with the rest of the module.