Python Programming/GUI Programming
||Please share your thoughts about whether to keep this page at Wikibooks.
Maintain this notice so this page can receive a fair review and people have time to boldly address reasonable concerns through concrete improvements. Remove or replace this notice after discussion concludes and a community decision is made. Please also consider notifying the primary contributors on their discussion page with
There are various GUI toolkits usable from Python.
See also book PyGTK For GUI Programming
PyGTK provides a convenient wrapper for the GTK+ library for use in Python programs, taking care of many of the boring details such as managing memory and type casting. The bare GTK+ toolkit runs on Linux, Windows, and Mac OS X (port in progress), but the more extensive features — when combined with PyORBit and gnome-python — require a GNOME install, and can be used to write full featured GNOME applications.
from PyQt4.QtCore import * from PyQt4.QtGui import * class App(QApplication): def __init__(self, argv): super(App, self).__init__(argv) self.msg = QLabel("Hello, World!") self.msg.show() if __name__ == "__main__": import sys app = App(sys.argv) sys.exit(app.exec_())
Bindings for the cross platform toolkit wxWidgets. WxWidgets is available on Windows, Macintosh, and Unix/Linux.
import wx class test(wx.App): def __init__(self): wx.App.__init__(self, redirect=False) def OnInit(self): frame = wx.Frame(None, -1, "Test", pos=(50,50), size=(100,40), style=wx.DEFAULT_FRAME_STYLE) button = wx.Button(frame, -1, "Hello World!", (20, 20)) self.frame = frame self.frame.Show() return True if __name__ == '__main__': app = test() app.MainLoop()
Dabo is a full 3-tier application framework. Its UI layer wraps wxPython, and greatly simplifies the syntax.
import dabo dabo.ui.loadUI("wx") class TestForm(dabo.ui.dForm): def afterInit(self): self.Caption = "Test" self.Position = (50, 50) self.Size = (100, 40) self.btn = dabo.ui.dButton(self, Caption="Hello World", OnHit=self.onButtonClick) self.Sizer.append(self.btn, halign="center", border=20) def onButtonClick(self, evt): dabo.ui.info("Hello World!") if __name__ == '__main__': app = dabo.ui.dApp() app.MainFormClass = TestForm app.start()
The "Hello World" example in pyFltk looks like:
from fltk import * window = Fl_Window(100, 100, 200, 90) button = Fl_Button(9,20,180,50) button.label("Hello World") window.end() window.show() Fl.run()
- PyKDE - Part of the kdebindings package, it provides a python wrapper for the KDE libraries.
- PyXPCOM provides a wrapper around the Mozilla XPCOM component architecture, thereby enabling the use of standalone XUL applications in Python. The XUL toolkit has traditionally been wrapped up in various other parts of XPCOM, but with the advent of libxul and XULRunner this should become more feasible.
- Graphic User Interface FAQ, python.org
- An excerpt from Chapter 20: GUI Development, from Python Programming on Win32, onlamp.com, by Mark Hammond, Andy Robinson; covers Tkinter, PythonWin and wxPython
- Google Ngram Viewer: Tkinter, wxPython, wxWidgets, PyGTK, PyQt