Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 系统托盘工具提示中的PyQt RichText格式_Python_User Interface_Pyqt_Tooltip_Richtext - Fatal编程技术网

Python 系统托盘工具提示中的PyQt RichText格式

Python 系统托盘工具提示中的PyQt RichText格式,python,user-interface,pyqt,tooltip,richtext,Python,User Interface,Pyqt,Tooltip,Richtext,这是简单系统托盘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.

这是简单系统托盘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()
在这段代码中,我想用一些图片和一些文本格式显示气球工具提示。为此,我使用RichText标记格式化。作为Ubuntu Linux系统Gnome桌面的结果,一切正常。但当我尝试在WindowsXP系统中使用RichText格式作为工具提示时,什么都不起作用。工具提示文本等于源字符串:。 Windows2.7上的Python版本,Linux2.6上的Python版本,但我认为问题不在不同的版本中


如果在Windows操作系统中RichText没有解析,我如何才能使相同类型的GUI更适合跨平台?

在Windows Qt上使用操作系统的工具提示系统,该系统只支持文本


如果您想要更高级的东西,可以使用如上所述的。您可能必须安装eventfilter或重写QTrayIcons事件方法才能获取帮助事件。

在Windows Qt上使用操作系统的工具提示系统,该系统仅支持文本


如果您想要更高级的东西,可以使用如上所述的。您可能需要安装eventfilter或重写QTrayIcons事件方法来获取帮助事件。

如果有人对创建balloon小部件感兴趣。这是我的代码:

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()

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

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()

感谢您的帮助,但不幸的是,QSystemTrayIcon.showMessage只能显示纯文本,不支持RichText。在毫秒时间内,为条目显示带有给定标题、消息和图标的气球消息。标题和消息必须是纯文本字符串。MessageIcon也是枚举,因此只能使用标准图片。可能存在其他决定?我能想到的唯一其他事情是创建一个无框架小部件来显示。感谢您的帮助,但不幸的是QSystemTrayIcon.showMessage只能显示纯文本,不支持RichText为具有给定标题的条目显示气球消息,以毫秒为单位指定时间的消息和图标。标题和消息必须是纯文本字符串。MessageIcon也是枚举,因此只能使用标准图片。可能还有其他的决定吗?我唯一能想到的是创建一个无框架的小部件。这不是ballonWidget中的第一行吗?\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu当然,缩进是错的,我已经修正了。如果不设置Qt转换器,tr方法将不起作用:您可以将其设置为UTF-8编码:QtCore.QTextCodec.setcodefortrqtcore.QTextCodec.codefornameutf-8+1好的,谢谢您让我知道。我完全不熟悉这个框架。ballonWidget中的第一行不是吗?\uuuu init\uuuuuuu缩进了一个额外的级别?我尝试了它,并且在我删除了show方法中对tr的调用后,它工作了。直接分配字符串。@SwiftsNamesake作为您的第一条注释:当然缩进是错误的,我已经修复了。如果不设置Qt转换器,tr方法将不起作用:您可以将其设置为UTF-8编码:QtCore.QTextCodec.setcodefortrqtcore.QTextCodec.codefornameutf-8+1好的,谢谢您让我知道。我对这个框架完全陌生。