开发者

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

开发者 https://www.devze.com 2023-01-04 07:41 出处:网络
I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual开发者_Go百科 basic and Java).

I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual开发者_Go百科 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 (i.e., 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


See this related SO answer on how to accomplish Windows system tray/OS X menu bar functionality in wxPython.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号