How to set a Tray Icon (tray icon) in a PyQt application?

3

I'm still new to PyQt and would like to clarify a question:

Is there any way to define a Tray Icon in a PyQt application? If so, how can I do it?

I need to set some options on this tray icon. Can you do that too?

    
asked by anonymous 11.11.2016 / 13:04

1 answer

3

The class QSystemTrayIcon is for this.

  

link

Assuming your application is qApp , here is a small example:

trayIcon = QtGui.QSystemTrayIcon( QtGui.QIcon('icone.png' ), qApp)
trayIcon.setTooltip('eu sou um ícone de bandeja')

sair = menu.addAction("Sair")
menu = QtGui.QMenu()

trayIcon.setContextMenu(menu)
trayIcon.show()

Points of interest:

  • setTooltip , as the name already says, serves to add a context menu to the icon. See the setContextMenu documentation for more details;

If you want to animate the icon, you can make calls to the QMenu method, using setIcon(const QIcon & icon) , for example. Just do not want to do anything too complex, not to sacrifice the responsiveness of OS.

Still, it's worth noting that the QTimer method exists if you want to display a message to the user. For example, in Windows 7, a balloon is used:

    
11.11.2016 / 13:11