简体   繁体   中英

Cross-platform Python GUI suitable for taskbar (Win) and menubar (mac) functionality?

I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual basic and Java). I've written some python code to screen-scrape data from a website, and now I want to build a GUI that will reside in the Mac OS X menubar, and in Window's task bar (ie, the system tray) .

The most useful general page on cross-plaform Python GUIs for me was this one (despite its name indication Window GUIs). And some stackoverflow questions came in useful as well (especially this one , and the accepted answer of this one about splitting up the GUI and cli code). I think I will go for either wxPython or QT because I want the GUI to look as native as possible.

However, as I've said the fairly simple GUI will mainly live in the taskbar/menubar. Should this influence my decision?

Here's an example for PyQt. This works for me on MacOS X; I haven't tried it on other platforms. Note that the QSystemTrayIcon class will raise exceptions if it doesn't have an icon – I grabbed the RSS feed svg from Wiki commons for my icon.svg (but you can give QIcon a PNG directly and not mess around with QtSvg ).

import PyQt4
from PyQt4 import QtCore, QtGui, QtSvg

app = QtGui.QApplication([])

i = QtGui.QSystemTrayIcon()

m = QtGui.QMenu()
def quitCB():
 QtGui.QApplication.quit()
def aboutToShowCB():
 print 'about to show'
m.addAction('Quit', quitCB)
QtCore.QObject.connect(m, QtCore.SIGNAL('aboutToShow()'), aboutToShowCB)
i.setContextMenu(m)

svg = QtSvg.QSvgRenderer('icon.svg')
if not svg.isValid():
 raise RuntimeError('bad SVG')
pm = QtGui.QPixmap(16, 16)
painter = QtGui.QPainter(pm)
svg.render(painter)
icon = QtGui.QIcon(pm)
i.setIcon(icon)
i.show()

app.exec_()

del painter, pm, svg # avoid the paint device getting
del i, icon          # deleted before the painter
del app

有关如何在wxPython中完成Windows系统托盘/ OS X菜单栏功能,请参阅此相关的SO答案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM