Using QSystemTrayIcon with PyQt

Alex Teiche xelapond at gmail.com
Tue Apr 1 00:00:33 EDT 2008


On Mar 31, 7:53 pm, Benjamin <musiccomposit... at gmail.com> wrote:
> On Mar 31, 8:41 pm, Alex Teiche <xelap... at gmail.com> wrote:
>
>
>
> > On Mar 31, 6:40 pm, Alex Teiche <xelap... at gmail.com> wrote:
>
> > > On Mar 31, 11:49 am, Alex Teiche <xelap... at gmail.com> wrote:
>
> > > > On Mar 30, 3:50 pm, Benjamin <musiccomposit... at gmail.com> wrote:
>
> > > > > On Mar 29, 11:02 pm, Alex Teiche <xelap... at gmail.com> wrote:> Hello,
>
> > > > > > I am pretty new to Python, and have never learned C++.  I am trying to
> > > > > > implement the following thing into my python application:
>
> > > > > >http://doc.trolltech.com/4.3/qsystemtrayicon.html
>
> > > > > > Through PyQt.  I have been using PyQt for awhile and I know how do use
> > > > > > it, but I could not get this specific thing to work.  Can someone give
> > > > > > me some hints as to get it working in Python?
>
> > > > > What problems are you having?
>
> > > > > > Thanks a ton,
>
> > > > > > Alex
>
> > > > Thanks everyone for your help.  I found the example to be particularly
> > > > helpful, and I have made a simplified version just to display an icon
> > > > with a quit button in its menu.  Once I know how to do that I will
> > > > incorporate it into my larger program, with more options and the
> > > > ability show messages.  The problem is, it doesn't work, and I can't
> > > > find out what's wrong.  Can you give me some hints?
>
> > > > Here is the code:
> > > > import sys
> > > > from PyQt4 import QtGui, QtCore
>
> > > > class trayIcon(QtGui.QWidget):
> > > >     def __init__(self, parent=None):
> > > >         QtGui.QWidget.__init__(self, parent)
>
> > > >         #********Create Actions for the Tray Menu********#
> > > >         self.quitAction = QtGui.QAction(self.tr("&Quit"), self)
> > > >         QtCore.QObject.connect(self.quitAction,
> > > > QtCore.SIGNAL("triggered()"), QtGui.qApp, QtCore.SLOT("quit()"))
>
> > > >         create_tray_icon()
>
> > > >         self.composeAction.setEnabled(visible)
> > > >         QtGui.QWidget.setVisible(self, visible)
>
> > > >         self.trayIcon.show()
>
> > > >     def create_tray_icon(self):
> > > >          self.trayIconMenu = QtGui.QMenu(self)
> > > >          self.trayIconMenu.addAction(self.composeAction)
> > > >          self.trayIcon = QtGui.QSystemTrayIcon(self)
> > > >          self.trayIcon.setContextMenu(self.trayIconMenu)
> > > >          self.trayIcon.setIcon(bad.svg)
>
> > > > app = QtGui.QApplication(sys.argv)
> > > > sys.exit(app.exec_())
>
> > > OK, I messed around with it some more, and it works.  I just don't
> > > know how to set an icon, and the example doesn't help at all.
>
> > > Here is the code:
> > > import sys
> > > from PyQt4 import QtCore, QtGui
>
> > > class Systray(QtGui.QWidget):
> > >     def __init__(self):
> > >         QtGui.QWidget.__init__(self)
>
> > >         self.createActions()
> > >         self.createTrayIcon()
>
> > >         #QtCore.QObject.connect(self.trayIcon,
> > > QtCore.SIGNAL("messageClicked()"), self.messageClicked)
> > >         #QtCore.QObject.connect(self.trayIcon,
> > > QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"),
> > > self.iconActivated)
>
> > >         self.trayIcon.show()
>
> > >     def createActions(self):
> > >         #self.minimizeAction = QtGui.QAction(self.tr("Mi&nimize"), self)
> > >         #QtCore.QObject.connect(self.minimizeAction,
> > >         #       QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("hide()"))
>
> > >         #self.maximizeAction = QtGui.QAction(self.tr("Ma&ximize"), self)
> > >         #QtCore.QObject.connect(self.maximizeAction,
> > >         #       QtCore.SIGNAL("triggered()"), self,
> > >         #       QtCore.SLOT("showMaximized()"))
>
> > >         #self.restoreAction = QtGui.QAction(self.tr("&Restore"), self)
> > >         #QtCore.QObject.connect(self.restoreAction,
> > > #               QtCore.SIGNAL("triggered()"), self,
> > >         #       QtCore.SLOT("showNormal()"))
>
> > >         self.quitAction = QtGui.QAction(self.tr("&Quit"), self)
> > >         QtCore.QObject.connect(self.quitAction, QtCore.SIGNAL("triggered()"),
> > >                 QtGui.qApp, QtCore.SLOT("quit()"))
>
> > >     def createTrayIcon(self):
> > >          self.trayIconMenu = QtGui.QMenu(self)
> > >          #self.trayIconMenu.addAction(self.minimizeAction)
> > >          #self.trayIconMenu.addAction(self.maximizeAction)
> > >          #self.trayIconMenu.addAction(self.restoreAction)
> > >          #self.trayIconMenu.addSeparator()
> > >          self.trayIconMenu.addAction(self.quitAction)
>
> > >          self.trayIcon = QtGui.QSystemTrayIcon(self)
> > >          self.trayIcon.setContextMenu(self.trayIconMenu)
>
> > > app = QtGui.QApplication(sys.argv)
> > > x = Systray()
> > > sys.exit(app.exec_())
>
> > > How would I go about setting the icon?
>
> > Sorry, here is the code with commented out lines removed:
>
> > import sys
> > from PyQt4 import QtCore, QtGui
>
> > class Systray(QtGui.QWidget):
> >     def __init__(self):
> >         QtGui.QWidget.__init__(self)
>
> >         self.createActions()
> >         self.createTrayIcon()
>
> >         self.trayIcon.show()
>
> >     def createActions(self):
>
> >         self.quitAction = QtGui.QAction(self.tr("&Quit"), self)
> >         QtCore.QObject.connect(self.quitAction, QtCore.SIGNAL("triggered()"),
> >                 QtGui.qApp, QtCore.SLOT("quit()"))
>
> >     def createTrayIcon(self):
> >          self.trayIconMenu = QtGui.QMenu(self)
> >          self.trayIconMenu.addAction(self.quitAction)
>
> >          self.trayIcon = QtGui.QSystemTrayIcon(self)
> >          self.trayIcon.setContextMenu(self.trayIconMenu)
>
> > app = QtGui.QApplication(sys.argv)
> > x = Systray()
> > sys.exit(app.exec_())
>
> I believe the method you want is QSystemTrayIcon.setIcon.

I found that, but what do I pass into it?  Passing a string to a .svg
file doesn't work.



More information about the Python-list mailing list