适用于任务栏 (Win) 和菜单栏 (mac) 功能的跨平台 Python GUI?

发布于 2024-09-06 11:11:29 字数 783 浏览 3 评论 0原文

我对 Python 编程相当陌生,对跨平台 GUI 构建也完全陌生(之前的 GUI 经验只是通过 Visual Basic 和 Java 获得的)。 我已经编写了一些 python 代码来从网站上抓取数据,现在我想构建一个 GUI ,它将驻留在 Mac OS X 菜单栏中和 Window 的任务栏(即系统托盘)< /em>.

对我来说,跨平台 Python GUI 最有用的通用页面是 这个(尽管它的名称表示Window GUI)。一些 stackoverflow 问题也很有用(尤其是 这个,以及 关于分割 GUI 和 cli 代码的公认答案)。 我想我会选择 wxPythonQT,因为我希望 GUI 看起来尽可能原生。

然而,正如我所说,相当简单的 GUI 主要位于任务栏/菜单栏中。 这会影响我的决定吗?

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 (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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2024-09-13 11:11:29

这是 PyQt 的示例。这对我在 MacOS X 上有效;我没有在其他平台上尝试过。请注意,如果 QSystemTrayIcon 类没有图标,则会引发异常 - 我抓住了 来自 Wiki commons 的 RSS feed svg,用于我的 icon.svg(但您可以给 QIcon直接 PNG,而不是与 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

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
软甜啾 2024-09-13 11:11:29

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文