PyQt provides a great set of libraries for rendering user interfaces that are truly cross platform. Being able to distribute to both Mac and PC is great for situations where the workforce may have a combination of platforms. Plus being a Python library, you also have the ability to interact directly with various software interfaces, such as Maya, Max or MotionBuilder. So how do you set it up? A straightforward operation, you might imagine. Just run:
pip install pyqt
Well that just won’t plug in any more. PyQt5 is the default install via pip nowadays, so many guides to installing PyQt4 that you’ll find on blogs don’t take that into account. The book that I started following, Rapid GUI Programming With Python And QT does feature instructions on how to install PyQt, and even they are out of date.
After running down many a cul-de-sac, I found one solid way of setting up PyQt4.
N.B. I got this method to work on both Mavericks and El Capitan, so it should be pretty solid.
If you haven’t already got Homebrew installed, you will need to have the command-line tools set up for XCode. Then you can install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
While executing this stage on a new computer, I was required to do further operations in order to complete this task:
- Run: sudo xcodebuild -license
- Agree with the Xcode license requirements.
- Rerun the Ruby command once this was complete.
Now you can go ahead and install PyQt4. You need to specify an exact path because of the PyQt5 default issue:
brew install cartr/qt4/pyqt
Next, you need to create clean install of Python 2.7 via Homebrew:
brew install python
Setting Up The Environment
PyQt is now installed, but you need to add the references to your Python environment. Brew has its installs in the folder, ‘/usr/local/Cellar’. Link your project to the Python executable in the cellar folder. My path was something like this:
Lastly, add the pyqt folder to your Python Path
You should now be able to get PyQt up and running in your IDE.
import sys from PyQt4 import QtGui def window(): app = QtGui.QApplication(sys.argv) w = QtGui.QWidget() b = QtGui.QLabel(w) b.setText('Hello World!') w.setGeometry(100,100,200,50) b.move(50,20) w.setWindowTitle('PyQt') w.show() sys.exit(app.exec_()) if __name__ == '__main__': window()