System tray applications using PyQt5

In this article, we will learn how to create a System Tray application using PyQt.

System Tray(or menu bar) is an area on the taskbar in an operating system. You can find this system on the bottom right side of the desktop if you’re using windows, and top right if using macOS. The icons which are visible in this notification area are the ones that run in the foreground. Some of the famous applications which use system tray to function, and they are Windscribe(VPN application) and Adobe Creative Cloud.

Menu bar applications are also useful to minimally control the desktop application using the shortcuts provided in the menu bar icon. One can choose to not open the whole application and still work by just using the options provided on the System Tray. In this article, you will learn how to create these applications.

Below is an example of an application named Windscribe.

Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
  
  
app = QApplication([])
app.setQuitOnLastWindowClosed(False)
  
# Adding an icon
icon = QIcon("icon.png")
  
# Adding item on the menu bar
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
  
# Creating the options
menu = QMenu()
option1 = QAction("Geeks for Geeks")
option2 = QAction("GFG")
menu.addAction(option1)
menu.addAction(option2)
  
# To quit the app
quit = QAction("Quit")
quit.triggered.connect(app.quit)
menu.addAction(quit)
  
# Adding options to the System Tray
tray.setContextMenu(menu)
  
app.exec_()

chevron_right


Output:

As you can see there is a marker icon on my mac menu bar, and there are three options visible namely Geeks for Geeks, GFG, and quit. By clicking on the last option(quit) you will exit the application.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.