PyQt: Show menu in a system tray application

前端 未结 6 1111
后悔当初
后悔当初 2020-12-02 08:45

First of all, I\'m an experienced C programmer but new to python. I want to create a simple application in python using pyqt. Let\'s imagine this application it is as simple

6条回答
  •  悲哀的现实
    2020-12-02 09:46

    Here is the code with Exit action implemented

    import sys
    from PyQt4 import QtGui, QtCore
    
    class SystemTrayIcon(QtGui.QSystemTrayIcon):
        def __init__(self, icon, parent=None):
           QtGui.QSystemTrayIcon.__init__(self, icon, parent)
           menu = QtGui.QMenu(parent)
           exitAction = menu.addAction("Exit")
           self.setContextMenu(menu)
           QtCore.QObject.connect(exitAction,QtCore.SIGNAL('triggered()'), self.exit)
    
        def exit(self):
          QtCore.QCoreApplication.exit()
    
    def main():
       app = QtGui.QApplication(sys.argv)
    
       w = QtGui.QWidget()
       trayIcon = SystemTrayIcon(QtGui.QIcon("qtLogo.png"), w)
    
       trayIcon.show()
       sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

提交回复
热议问题