Skip to content

[PyQt5] How to avoid the developed interface from displaying icons in the sidebar of the desktop

I have been trying to create a simple desktop clock before, and thanks to that, I have learned a lot about PyQt5, a framework for making interface in Python.

Today I want to record how to avoid to display interface icon in the sidebar of the desktop. I actually wanted to complete this function for a long time, and occasionally searched the Internet, but I haven’t found a way until now.


How to hide icon

Before starting, I want to use a program to demo my problem.

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *


class MainWindow(QWidget):
     def __init__(self):
         super(MainWindow, self).__init__()
         self.resize(500, 500)
         self.setWindowTitle('test')


if __name__ == '__main__':
     app = QApplication([])
     window = MainWindow()
     window.show()
     sys.exit(app.exec_())



Output:

The green frame is the interface I created using PyQt5, and the red frame is the icon in the sidebar of my desktop (It’s Gnome3, Mac’s approach is different).

To put it simply, suppose this is a small tool that is executed in the background.

It is actually very simple to do this.

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt


class MainWindow(QWidget):
     def __init__(self):
         super(MainWindow, self).__init__()
         self.resize(500, 500)
         self.setWindowTitle('test')
         self.setWindowFlags(Qt.Tool)


if __name__ == '__main__':
     app = QApplication([])
     window = MainWindow()
     window.show()
     sys.exit(app.exec_())



That’s right! That is to import the Qt module and use

self.setWindowFlags(Qt.Tool)



This line code can be set. The displayed effect is as follows:

You can see that the green program interface still exists, but the red framed part (where the Icon icon originally exists) is empty.


References

Leave a Reply