PyQt4 有没有办法最小化到托盘?我已经使用过 QSystemTrayIcon 类,但现在我想最小化或“隐藏”我的应用程序窗口,并仅显示托盘图标。

有人做过这个吗?任何方向将不胜感激。

在 Window XP Pro 上使用 Python 2.5.4 和 PyQt4

有帮助吗?

解决方案

一旦你记住没有办法真正最小化到 系统托盘.

相反,你可以通过这样做来伪造它:

  1. 捕获窗口上的最小化事件
  2. 在最小化事件处理程序中,创建并显示 QSystemTrayIcon
  3. 同样在最小化事件处理程序中,在窗口上调用 hide() 或 setVisible(false)
  4. 捕获系统托盘图标上的单击/双击/菜单项
  5. 在系统托盘图标事件处理程序中,在窗口上调用 show() 或 setVisible(true),并可选择隐藏托盘图标。

其他提示

代码帮助,所以这里的东西,我写了一个应用程序,除中的closeEvent而不是减少事件。

注意:

“的closeEvent(事件)”是一个覆盖Qt的事件,所以它必须在实现你想要隐藏的窗口类付诸表决。

“okayToClose()”是可以考虑的功能实现(或者你可能要存储一个布尔标志),因为有时候你真的想退出,而不是减少到系统托盘中的应用。

有也是如何显示()的示例的窗口再次

def __init__(self):
  traySignal = "activated(QSystemTrayIcon::ActivationReason)"
  QtCore.QObject.connect(self.trayIcon, QtCore.SIGNAL(traySignal), self.__icon_activated)

def closeEvent(self, event):
  if self.okayToClose(): 
    #user asked for exit
    self.trayIcon.hide()
    event.accept()
  else:
    #"minimize"
    self.hide()
    self.trayIcon.show() #thanks @mojo
    event.ignore()

def __icon_activated(self, reason):
  if reason == QtGui.QSystemTrayIcon.DoubleClick:
    self.show()

只需由Chris添加到示例:

根据关键你声明信号时,使用了Qt符号即

<强>正确

self.connect(self.icon, SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.iconClicked)

和不PyQt的一个

<强>不正确并不会工作:

self.connect(self.icon, SIGNAL("activated(QSystemTrayIcon.ActivationReason)"), self.iconClicked)

请注意在信号串的::。这花了我大约三个小时弄清楚。

下面的工作code..Thanks Matze 对于关键中,SIGNAL我花了更多的时间的好奇..但做其他的事情。所以TA的#!时刻: - )

def create_sys_tray(self):
    self.sysTray = QtGui.QSystemTrayIcon(self)
    self.sysTray.setIcon( QtGui.QIcon('../images/corp/blip_32.png') )
    self.sysTray.setVisible(True)
    self.connect(self.sysTray, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.on_sys_tray_activated)

    self.sysTrayMenu = QtGui.QMenu(self)
    act = self.sysTrayMenu.addAction("FOO")

def on_sys_tray_activated(self, reason):
    print "reason-=" , reason

这是vzades响应的编辑,但它被驳回若干理由。它做同样的事情作为自己的代码也将遵循尽量减少事件(和没有语法错误运行/缺少的图标)。

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        style = self.style()

        # Set the window and tray icon to something
        icon = style.standardIcon(QtGui.QStyle.SP_MediaSeekForward)
        self.tray_icon = QtGui.QSystemTrayIcon()
        self.tray_icon.setIcon(QtGui.QIcon(icon))
        self.setWindowIcon(QtGui.QIcon(icon))

        # Restore the window when the tray icon is double clicked.
        self.tray_icon.activated.connect(self.restore_window)

    def event(self, event):
        if (event.type() == QtCore.QEvent.WindowStateChange and 
                self.isMinimized()):
            # The window is already minimized at this point.  AFAIK,
            # there is no hook stop a minimize event. Instead,
            # removing the Qt.Tool flag should remove the window
            # from the taskbar.
            self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)
            self.tray_icon.show()
            return True
        else:
            return super(Example, self).event(event)

    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(
            self,
            'Message',"Are you sure to quit?",
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
            QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            self.tray_icon.show()
            self.hide()
            event.ignore()

    def restore_window(self, reason):
        if reason == QtGui.QSystemTrayIcon.DoubleClick:
            self.tray_icon.hide()
            # self.showNormal will restore the window even if it was
            # minimized.
            self.showNormal()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

这是处理对PyQt5托盘图标双击的正确方法。

def _create_tray(self):
    self.tray_icon = QSystemTrayIcon(self)
    self.tray_icon.activated.connect(self.__icon_activated)

def __icon_activated(self, reason):
    if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick):
        pass

这是代码,它确实帮助下,我相信我秀的代码

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QSystemTrayIcon


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.icon = QSystemTrayIcon()
        r = self.icon.isSystemTrayAvailable()
        print r
        self.icon.setIcon(QtGui.QIcon('/home/vzades/Desktop/web.png'))
        self.icon.show()
        # self.icon.setVisible(True)
        self.setGeometry(300, 300, 250, 150)
        self.setWindowIcon(QtGui.QIcon('/home/vzades/Desktop/web.png'))
        self.setWindowTitle('Message box')
        self.show()
        self.icon.activated.connect(self.activate)
        self.show()

    def closeEvent(self, event):

        reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to quit?", QtGui.QMessageBox.Yes |
                                           QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            self.icon.show()

            self.hide()

            event.ignore()

    def activate(self, reason):
        print reason
        if reason == 2:
            self.show()

    def __icon_activated(self, reason):
        if reason == QtGui.QSystemTrayIcon.DoubleClick:
            self.show()


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top