PyQt: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
Line 28: Line 28:


1 Design a user interface in QT4 Designer. Save as a .ui file (XML).
1 Design a user interface in QT4 Designer. Save as a .ui file (XML).
2 Generate Python code from the UI file using pyuic.
2 Generate Python code from the UI file using pyuic.


Line 45: Line 46:
   
   
  sys.exit(app.exec_())
  sys.exit(app.exec_())
This suffices if you don't need to call any Python code from your user interface. (I suppose that ''could'' happen somehow?) This example shows how to call code from buttons.
<pre>
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
from DispatchCenter import Ui_MainWindow
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #self.tableView
        # Connect up the buttons.
        self.connect(self.ui.btnSend, QtCore.SIGNAL("clicked()"),
                    self, QtCore.SLOT("sendDispatch()"))
        self.connect(self.ui.btnCustom, QtCore.SIGNAL("clicked()"),
                    self, QtCore.SLOT("openCustomForm()"))
    def sendDispatch():
        # do something here
        pass
   
    def openCustomForm():
        # do something here
        pass
   
app = QtGui.QApplication(sys.argv)
window = MainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
</pre>

Revision as of 21:12, 2 September 2008

  1. I seek a way to design GUI interfaces for Python.
  2. I want one that runs cross-platform (Windows and Linux)
  3. I want to create apps that can run on my OpenMoko FreeRunner.

This page is about PyQt, which is a set of bindings to allow using Python with TrollTech QT

Articles and references

At DevShed: PyQT Getting Started

Some tutorials:

Creating the UI programmatically: http://zetcode.com/tutorials/pyqt4/

PyQt 3 (outdated): http://www.cs.usfca.edu/~afedosov/qttut/

The official documentation: http://www.riverbankcomputing.com/static/Docs/PyQt4/pyqt4ref.html

Development tools

I use ActiveState Komodo as my Python IDE. I recommend it highly even though it costs money. There is a CIX package for Komodo available from ActiveState.

Windows A complete Windows binary package is available from http://www.riverbankcomputing.co.uk/pyqt It includes everything you need, including the runtime, the designer, the bindings. Quite nice.

Development cycle

1 Design a user interface in QT4 Designer. Save as a .ui file (XML).

2 Generate Python code from the UI file using pyuic.

pyuic4.bat myForm.ui myForm.py

3 Create a python wrapper with the event loop in it. For example

import sys
from PyQt4 import QtGui
from myForm import Ui_MainWindow
        
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()

sys.exit(app.exec_())

This suffices if you don't need to call any Python code from your user interface. (I suppose that could happen somehow?) This example shows how to call code from buttons.

#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui
from DispatchCenter import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        #self.tableView

        # Connect up the buttons.
        self.connect(self.ui.btnSend, QtCore.SIGNAL("clicked()"),
                     self, QtCore.SLOT("sendDispatch()"))
        self.connect(self.ui.btnCustom, QtCore.SIGNAL("clicked()"),
                     self, QtCore.SLOT("openCustomForm()"))

    def sendDispatch():
        # do something here
        pass
    
    def openCustomForm():
        # do something here
        pass
    
app = QtGui.QApplication(sys.argv)
window = MainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())