Setting the Windows taskbar icon in PyQt

前端 未结 4 1782
小鲜肉
小鲜肉 2020-12-09 08:10

I\'m working on an applcation in Python\'s PyQt4 and cannot find how to change the taskbar icon. I made my .ui files in Qt\'s Designer, where I can change the windowIc

相关标签:
4条回答
  • 2020-12-09 08:39

    For me, the following code works for both changing task bar icon and window icon

    win.setWindowIcon(QIcon('logo.png'))
    
    0 讨论(0)
  • 2020-12-09 08:42

    This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.

    See this answer for details, along with a workaround using ctypes.

    0 讨论(0)
  • 2020-12-09 08:43

    It seems to me that the problem may be caused by lack of icon with the right size. The following setup worked for me in PyQT4:

    # set app icon    
    app_icon = QtGui.QIcon()
    app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
    app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
    app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
    app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
    app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
    app.setWindowIcon(app_icon)
    

    I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.

    0 讨论(0)
  • 2020-12-09 08:48

    You need to call setWindowIcon(...) on the window, not on the application.

    Here's an example, which works for me:

    #!/usr/bin/env python3
    
    import os
    import sys
    import subprocess
    import os.path
    
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    class MyWin(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MyWin, self).__init__(parent)
            self.setWindowTitle("My Window")
            self.setWindowIcon(QtGui.QIcon('test_icon.png'))
            self.show()
    
    def main(args):
        app = QtGui.QApplication([])
    
        ww= MyWin()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main(sys.argv[1:])
    
    0 讨论(0)
提交回复
热议问题