PYQT4 : QSystemTrayIcon练习

照着demo自己做了一遍,练练手

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore class SysTray(QtGui.QDialog):
    def __init__(self, parent=None):
        super(SysTray, self).__init__(parent)
        self.createIconGroupBox()
        self.createMessageGroupBox()
        self.createActions()
        self.createSysTray()         mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.iconGroupBox)
        mainLayout.addWidget(self.messageGroupBox)
        self.setLayout(mainLayout)         self.showMessageButton.clicked.connect(self.showMessage)
        self.iconCheckBox.stateChanged.connect(self.showIcon)
        # 即时刷新Tray Icon
        self.iconComboBox.currentIndexChanged.connect(self.setIcon)
        self.iconComboBox.setCurrentIndex(1)         self.trayIcon.activated.connect(self.iconActivated)         self.trayIcon.show()
        self.resize(400, 300)
        self.setWindowTitle("Systray")     def setIcon(self, index):
        icon = self.iconComboBox.itemIcon(index)
        self.trayIcon.setIcon(icon)
        self.setWindowIcon(icon)
        self.trayIcon.setToolTip(self.iconComboBox.itemText(index))     def showIcon(self, i):
        # Unchecked:i=0 Checked=2
        if i == QtCore.Qt.Unchecked:
            self.trayIcon.hide()
        else:
            self.trayIcon.show()     def iconActivated(self, reason):
        # 切换图标
        if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
            max = self.iconComboBox.count()
            current = self.iconComboBox.currentIndex() + 1
            if current == max:
                current = 0
            self.iconComboBox.setCurrentIndex(current)     def showMessage(self):
        icon = self.typeComboBox.itemData(self.typeComboBox.currentIndex())
        self.trayIcon.showMessage(self.titleLine.text(), self.bodyText.toPlainText(), icon, self.durationSpin.value())     def closeEvent(self, event):
        # 重新定义“X”动作,如果iconCheckBox unchecked 就关闭窗口
        if self.trayIcon.isVisible():
            QtGui.QMessageBox.information(self, "SysTray",
                                          "The program will keep running in the system tray. To "
                                          "terminate the program, choose <b>Quit</b> in the "
                                          "context menu of the system tray entry.")
            self.hide()
            event.ignore()     def createActions(self):
        self.minimizeAction = QtGui.QAction("Mi&nimize", self, triggered=self.showMinimized)
        self.maximumAction = QtGui.QAction("Ma&ximum", self, triggered=self.showMaximized)
        self.restoreAction = QtGui.QAction("&Restore", self, triggered=self.showNormal)
        self.quitAction = QtGui.QAction("&Quit", self, triggered=QtGui.qApp.quit)     def createSysTray(self):
        self.trayIconMenu = QtGui.QMenu(self)
        self.trayIconMenu.addAction(self.minimizeAction)
        self.trayIconMenu.addAction(self.maximumAction)
        self.trayIconMenu.addAction(self.restoreAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.quitAction)         self.trayIcon = QtGui.QSystemTrayIcon(self)
        self.trayIcon.setContextMenu(self.trayIconMenu)     def createIconGroupBox(self):
        self.iconGroupBox = QtGui.QGroupBox('Tray Box')         self.iconLabel = QtGui.QLabel('Icon: ')
        self.iconComboBox = QtGui.QComboBox()
        self.iconComboBox.addItem(QtGui.QIcon('resource/heart.svg'), 'heart')
        self.iconComboBox.addItem(QtGui.QIcon('resource/trash.svg'), 'trash')
        self.iconComboBox.addItem(QtGui.QIcon('resource/bad.svg'), 'bad')         self.iconCheckBox = QtGui.QCheckBox('Show Icon')
        self.iconCheckBox.setChecked(True)         iconLayout = QtGui.QHBoxLayout()
        iconLayout.addWidget(self.iconLabel)
        iconLayout.addWidget(self.iconComboBox)
        iconLayout.addStretch()
        iconLayout.addWidget(self.iconCheckBox)         self.iconGroupBox.setLayout(iconLayout)     def createMessageGroupBox(self):
        self.messageGroupBox = QtGui.QGroupBox('Ballon Message')
        self.typeLabel = QtGui.QLabel('Type: ')
        #addItem (QString, QVariant)
        #addItem (QIcon, QString, QVariant)
        #QVariant QComboBox.itemData(int)
        #这里的QVariant很重要,保存了大量大数据类型
        self.typeComboBox = QtGui.QComboBox()
        self.typeComboBox.addItem("None", QtGui.QSystemTrayIcon.NoIcon)
        self.typeComboBox.addItem(
            self.style().standardIcon(QtGui.QStyle.SP_MessageBoxInformation), 'Informathion',
            QtGui.QSystemTrayIcon.Information)
        self.typeComboBox.addItem(
            self.style().standardIcon(QtGui.QStyle.SP_MessageBoxWarning), 'Warniing', QtGui.QSystemTrayIcon.Warning)
        self.typeComboBox.addItem(
            self.style().standardIcon(QtGui.QStyle.SP_MessageBoxCritical), 'Critical', QtGui.QSystemTrayIcon.Critical)
        self.typeComboBox.setCurrentIndex(1)         self.durationLabel = QtGui.QLabel('Duration: ')
        self.durationSpin = QtGui.QSpinBox()
        self.durationSpin.setSuffix('s')
        self.durationSpin.setRange(5, 15)
        self.durationDescLabel = QtGui.QLabel('some systems might ignore this hint')
        self.titleLabel = QtGui.QLabel('Title')
        self.titleLine = QtGui.QLineEdit('Cannot connect to network')
        self.bodyLabel = QtGui.QLabel('Body: ')
        self.bodyText = QtGui.QTextEdit()
        self.bodyText.setPlainText("Don't believe me. Honestly, I don't have "
                                   "a clue.\nClick this balloon for details.")
        self.showMessageButton = QtGui.QPushButton('Show Message')
        self.showMessageButton.setDefault(True)         messageGroupLayout = QtGui.QGridLayout()
        messageGroupLayout.addWidget(self.typeLabel, 0, 0)
        messageGroupLayout.addWidget(self.typeComboBox, 0, 1, 1, 2)
        messageGroupLayout.addWidget(self.durationLabel, 1, 0)
        messageGroupLayout.addWidget(self.durationSpin, 1, 1)
        messageGroupLayout.addWidget(self.durationDescLabel, 1, 2, 1, 3)
        messageGroupLayout.addWidget(self.titleLabel, 2, 0)
        messageGroupLayout.addWidget(self.titleLine, 2, 1, 1, 4)
        messageGroupLayout.addWidget(self.bodyLabel, 3, 0)
        messageGroupLayout.addWidget(self.bodyText, 3, 1, 2, 4)
        messageGroupLayout.addWidget(self.showMessageButton, 5, 4)
        # 设置拉伸的基准
        messageGroupLayout.setColumnStretch(3, 1)
        messageGroupLayout.setRowStretch(4, 1)         self.messageGroupBox.setLayout(messageGroupLayout) if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    tray = SysTray()
    tray.show()
    sys.exit(app.exec_())

PYQT4 : QSystemTrayIcon练习

上一篇:Oracle 11g 单实例静默安装实战记录(linux)


下一篇:ES6中的Symbol类型