PyQt4

Czy jest sposób na zminimalizowanie tray ' a w PyQt4? Pracowałem już z klasą QSystemTrayIcon, ale teraz chciałbym zminimalizować lub" ukryć " moje okno aplikacji i pokazać tylko ikonę w tacce.

Czy ktoś to zrobił? Każdy kierunek będzie mile widziany.

Używanie Pythona 2.5.4 i PyQt4 na Windowsie XP Pro

Author: Gavin Vickery, 2009-04-17

7 answers

Jest to dość proste, gdy przypomnisz sobie, że nie ma sposobu na zminimalizowanie do zasobnika systemowego.

Zamiast tego udawaj, robiąc to:

  1. Złap Zdarzenie Minimalizuj w oknie
  2. w module obsługi zdarzenia Minimalizuj, Utwórz i pokaż qsystemtrayicon
  3. również w module obsługi zdarzenia Minimalizuj wywołanie hide () lub setVisible (false) w oknie
  4. Złap kliknięcie / podwójne kliknięcie / element menu na ikonie w tacce systemowej
  5. w Twoim systemie obsługa zdarzeń ikony w tacce, wywołanie show() lub setVisible (true)w oknie i opcjonalnie ukrycie ikony w tacce.
 29
Author: Kevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-04-16 22:53:20

Kod pomaga, więc oto coś, co napisałem dla aplikacji, z wyjątkiem closeEvent zamiast zdarzenia Minimalizuj.

Uwagi:

" closeEvent (event)" jest nadpisanym zdarzeniem Qt, więc musi być umieszczone w klasie, która implementuje okno, które chcesz ukryć.

" okayToClose ()" jest funkcją, którą możesz rozważyć implementację (lub flagę logiczną, którą możesz zapisać) , ponieważ czasami chcesz zamknąć aplikację zamiast minimalizować do systray.

Jest też przykład jak ponownie pokazać () swoje okno.

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()
 12
Author: Chris Cameron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-19 18:09:22

Aby dodać do przykładu przez Chrisa:

Ważne jest , aby używać notacji Qt przy deklarowaniu sygnału, tzn.

Poprawne :

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

A nie PyQt

Niepoprawne i nie działa:

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

Zwróć uwagę na :: w ciągu sygnału. Zajęło mi to około trzech godzin, żeby się tego dowiedzieć.

 7
Author: WrongAboutMostThings,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-01 22:34:35

Oto działający kod..Dzięki [2]} Matze [3]} za [2]} Crucial , sygnał zabrał mi więcej godzin ciekawości.. ale robienie innych rzeczy. so ta for a #! moment : -)

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
 4
Author: PedroMorgan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-13 05:01:05

Była to edycja odpowiedzi vzadesa, ale została odrzucona z wielu powodów. Robi dokładnie to samo co ich kod, ale będzie również posłuszny zdarzeniu Minimalizuj (i działa bez błędów składniowych/brakujących ikon).

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()
 3
Author: Jordan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-04 01:56:58

This is the code and it does help I believe in show me the code

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()
 1
Author: vzades,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-29 02:20:06

Jest to prawidłowy sposób obsługi podwójnego kliknięcia na ikonę w tacce dla 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
 0
Author: Jorjon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-28 11:50:57