English 中文(简体)
PyQt 系统托盘工具提示中的 PyQt RichText 格式化
原标题:PyQt RichText Formatting in system tray tooltip

这是简单系统托盘 PyQt 应用程序的代码示例 。

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.

在 Windows OS RichText 中, 如果 Windows OS RichText 没有解析, 我如何制作相同的图形界面?

最佳回答

如果有人也有兴趣创建气球部件。 这是我的代码:

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()
问题回答

在 Windows Qt 上使用只支持文字的 os tooltip 系统 。

如果您想要更先进的东西, 您可以 < a href="http://qt- project. org/ doc/ qsyrayicon.html#showMessage" rel="no follow"\\ code\ systemTrayIcon.showMessage () 使用所谓的 < a href=" http://qt- project.org/doc/desktop-systray.html" rel="nofol" > here 。 您可能必须安装事件过滤器或推翻 < event 方法来获取帮助事件。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...