【问题标题】:Cross-platform Python GUI suitable for taskbar (Win) and menubar (mac) functionality?适合任务栏 (Win) 和菜单栏 (mac) 功能的跨平台 Python GUI?
【发布时间】:2010-06-23 19:21:20
【问题描述】:

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

对我来说,关于跨平台 Python GUI 的最有用的通用页面是 this one(尽管它的名称表示 Window GUI)。一些 stackoverflow 问题也很有用(尤其是 this onethe accepted answer of this one 关于拆分 GUI 和 cli 代码)。 我想我会选择 wxPythonQT,因为我希望 GUI 看起来尽可能原生。

但是,正如我所说,相当简单的 GUI 将主要存在于任务栏/菜单栏中。 这会影响我的决定吗?

【问题讨论】:

    标签: python user-interface cross-platform taskbar menubar


    【解决方案1】:

    这是 PyQt 的一个示例。这适用于我在 MacOS X 上;我没有在其他平台上尝试过。请注意,如果QSystemTrayIcon 类没有图标,它会引发异常——我为我的icon.svg 抓取了RSS feed svg from Wiki commons(但你可以直接给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
    

    【讨论】:

      【解决方案2】:

      请参阅related SO answer,了解如何在 wxPython 中完成 Windows 系统托盘/OS X 菜单栏功能。

      【讨论】:

        猜你喜欢
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多