简体   繁体   中英

Python - cross-platform wrapper for GUI libraries?

I have seen through python - Pygtk VS Pyqt VS WxPython VS Tkinter ; and my question is slightly different.

For instance, I use Ubuntu Gnome, there python-qt is not by default installed; and so if I want to use a python-qt application, I have to download python-qt (plus qt libraries); I'd expect it may be something similar for KDE (or other) desktops...

Well, often I'd need to produce a very simple GUI, and I'd like it to be able to "run anywhere" where there is Python... However, if the user already has some of these libraries, I'd say, why not use them? For complicated stuff, obviously an all-encompassing wrapper would not be viable (after all, all those libraries are cross-platform) - but for simple stuff, like the "hello work" examples below, maybe there is something that already exists?

In comparison to the examples below, I'd imagine something like (pseudocode):

...
appgui = getCrossPlatformGUI()
mw = appgui.getMainWindow()
button = appgui.getButton(args)
appgui.connect(button, args)
appgui.show(button)
...

... where getCrossPlatformGUI() on Linux would first look for python-qt , if not found then python-gtk , then wxWindows , then tkInter (on Windows maybe in a different order, etc) - and would deliver a window/application of the library that has been found on the system, defaulting in all cases with tkInter (which, as I understand, is always built in python).

Many thanks in advance for any answers,
Cheers!

hello-pyqt.py ( HELLO-PyQt的-ubuntu.png , HELLO-PyQt的-opensuse.png )

#!/usr/bin/env python
# http://en.wikibooks.org/wiki/Python_Programming/PyQt4#Hello.2C_world.21

import sys
from PyQt4 import Qt, QtCore

def sayHello():
  print "Hello, World!"

#####

a = Qt.QApplication(sys.argv)

hellobutton = Qt.QPushButton("Say 'Hello world!'", None)

a.connect(hellobutton, Qt.SIGNAL("clicked()"), sayHello)
hellobutton.clicked.connect(QtCore.QCoreApplication.instance().quit)

hellobutton.show()

a.exec_()

hello-pygtk.py ( HELLO-PyGTK的,ubuntu.png , HELLO-PyGTK的,opensuse.png )

#!/usr/bin/env python
# http://www.pygtk.org/pygtk2tutorial/examples/helloworld.py

import pygtk
pygtk.require('2.0')
import gtk

# This is a callback function. The data arguments are ignored
# in this example. More on callbacks below.
def sayHello(widget, data=None):
  print "Hello, World!"

def destroy(widget, data=None):
  gtk.main_quit()

#####

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", destroy)

hellobutton = gtk.Button("Say 'Hello world!'")

hellobutton.connect("clicked", sayHello, None)
hellobutton.connect_object("clicked", gtk.Widget.destroy, window)

window.add(hellobutton)
hellobutton.show()
window.show()

gtk.main()

The library called wxWidgets was created to do exactly that; the “wx” stands for Windows and X. Their history page has more info. The Python bindings for it are wxPython, as you probably know.

Apparently by now it feels like another heavyweight GUI library, but it “just” wraps native GTK/Qt/Windows widgets.

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