Why isn't .ico file defined when setting window's icon?

前端 未结 12 1575
一整个雨季
一整个雨季 2020-11-27 15:47

When I tried to change the window icon in the top left corner from the ugly red \"TK\" to my own favicon using the code below, Python threw an error:

from tk         


        
12条回答
  •  一个人的身影
    2020-11-27 16:18

    So it looks like root.iconbitmap() only supports a fixed directory.
    sys.argv[0] returns the directory that the file was read from so a simple code would work to create a fixed directory.

    import sys
    def get_dir(src):
        dir = sys.argv[0]
        dir = dir.split('/')
        dir.pop(-1)
        dir = '/'.join(dir)
        dir = dir+'/'+src
        return dir
    

    This is the output

    >>> get_dir('test.txt')
    'C:/Users/Josua/Desktop/test.txt'
    

    EDIT:
    The only issue is that this method dosn't work on linux

    josua@raspberrypi:~ $ python
    Python 2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.argv[0]
    ''
    >>>
    

提交回复
热议问题