PyQt RichText Formatting in system tray tooltip
-
10-06-2021 - |
题
This is an example of code for simple system tray PyQt application.
import sys
from PyQt4 import QtGui
def main():
app = QtGui.QApplication(sys.argv)
trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon('test.png'), app)
menu = QtGui.QMenu()
exitAction = menu.addAction("Exit")
trayIcon.setContextMenu(menu)
# I'd like to show picture in tooltip, BUT IT'S NOT WORK IN WINDOWS
trayIcon.setTooltip('<img src="SomePicture.png" width="48" height="48"/>')
trayIcon.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
In this code I'd like to show balloon tooltip with a some picture and some kind of text formating. For this purpose I use RichText tags formatting. As the result for Ubuntu Linux system (Gnome desktop) everything is Ok. But when I try use RichText formatting for tooltip in Windows XP system, nothing works. Tooltip text equals source string: ''. Python version on Windows 2.7, on Linux 2.6 but I think that problem is not in different versions.
If in Windows OS RichText isn't parsing how can I make same kind of GUI (Crossplatform is prefered)?
解决方案 2
If someone also interested in create of balloon widget. This my code:
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, parent = None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
traySignal = "activated(QSystemTrayIcon::ActivationReason)"
self.connect(self, QtCore.SIGNAL(traySignal), self._activateRoutine)
self.balloon = balloonWidget(name)
def _activateRoutine(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger:
self.balloon.show(self.geometry())
class balloonWidget(QtGui.QWidget):
def __init__(self,name):
QtGui.QWidget.__init__(self, parent = None, flags = QtCore.Qt.Popup)
self.name = name
self.offsetX = 10
self.offsetY = 10
self.outInfo = QtGui.QLabel(self)
self.setStyleSheet("QWidget {border:5px solid rgb(170, 170, 255);}")
def show(self,coord):
richText = tr('Any text with Rich Format')
self.outInfo.setText(richText)
self.outInfo.show()
self.adjustSize()
origin = QtGui.QDesktopWidget().availableGeometry().bottomRight()
if coord.y() < origin.y()/2:
moveY = coord.bottomLeft().y() + self.offsetY
else:
moveY = coord.topLeft().y() - (self.height() + self.offsetY)
if coord.x() + self.width() + self.offsetX >= origin.x():
moveX = origin.x() - (self.width() + self.offsetX)
else:
moveX = coord.x()
self.move(moveX,moveY)
self.setVisible(True)
def closeEvent(self, event):
event.ignore()
self.hide()
def mousePressEvent(self, event):
self.close()
其他提示
On Windows Qt uses the os' tooltip system, which only supports text.
If you want something more advanced, you could QSystemTrayIcon.showMessage()
use as described here. You'll probably have to install an eventfilter or override the QTrayIcon
s event
method to get the help event.