System tray applications using PyQt5
Last Updated :
22 May, 2020
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:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication([])
app.setQuitOnLastWindowClosed( False )
icon = QIcon( "icon.png" )
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible( True )
menu = QMenu()
option1 = QAction( "Geeks for Geeks" )
option2 = QAction( "GFG" )
menu.addAction(option1)
menu.addAction(option2)
quit = QAction( "Quit" )
quit.triggered.connect(app.quit)
menu.addAction(quit)
tray.setContextMenu(menu)
app.exec_()
|
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.
Share your thoughts in the comments
Please Login to comment...