Saturday, November 6, 2010

Getting Interactive With PyQt

One of the cooler features with PyQt (IMHO) is the possibility to write Qt code interactively. It isn't actually something new and I'm sure it's been there for a while but some of you might not be aware of it. I find it very useful when I want to learn new things in Qt, so without further ado, here's a short introduction on using it.

Start an interactive python session by executing python in a shell:
$> python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Let's create a simle top-level window (I wont add the '>>>' in case you would like to copy/paste the code):
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication([])
window = QWidget()
window.resize(400, 300)
window.show()
Now you should have a window similar to the screen shot below:

Ok, let's create a push button:
button = QPushButton("Click Me!", window)
Hmm, no button shows up... Why? Well, that's because we need to make it visible after we have added it to the window:
button.show()
Ahh, sweet! We have added the button to the window interactively:
Let's put the button at the center of the window by using a layout manager:

layout = QHBoxLayout(window)
layout.addWidget(button)


And finally, let's see how 'layout.addStretch()' affects the layout:
layout.addStretch()

You can easily add a slot to the button's clicked signal:
def mySlot():
    print "Button Clicked"
button.clicked.connect(mySlot)
As you can see, it's an easy way to try out thing in Qt.

Thanks for reading!

10 comments:

  1. If you use ipython (http://ipython.scipy.org/moin/) then you can run the Qt event loop in the background with the -q4thread command line option. This lets you use the GUI that you build exactly as if it was running in a real application.

    There is also -wthread for wxPython and -gthread for GTK event loops.

    ReplyDelete
  2. You can also use stackless or greenlets to have background loops, it's also a tad more pythonic.

    ReplyDelete
  3. This isn't particular to pyQt.
    Take a look at iPython. You can do this with other gui frameworks as well.

    ReplyDelete
  4. But PyQt supports this out of the box for the standard python shell by installing an input hook which processes events while the interpreter is waiting for input. This doesn't require any additional software to be installed to work, see http://www.riverbankcomputing.com/static/Docs/PyQt4/pyqt4ref.html#using-pyqt-from-the-python-shell

    I was doing some googling on 'pygtk interactively' and I couldn't find anything that pygtk supports this in the standard python interpreter shell, am I wrong?

    ReplyDelete
  5. i know this is not the place to ask for help but i need it so desperately. I hope you won't mind if I ask it here only.

    I have a QGraphicTextItem on a QGraphicScene. I want to get the coordinates of the textCursor in scene coordinates. how can i get that?

    btw I love your blog tat's why i follow it... :) plz keep up this work... at least for me... :D

    ReplyDelete
  6. Wish: I'm glad that you like my blog.

    I haven't played a lot with QGraphics because I prefer using traditional widget or QML, much easier :)

    I'm not sure if it's possible to find out the cursor's position in scene coords since the TextCursor isn't a GraphicsObject so it doesn't know anything about the scene. It only knows about the position within a TextDocument.

    I can recommend you to sign up at http://developer.qt.nokia.com and submit your question. It's a friendly place and there are a lot of trolls hanging around so they should be able to answer your question.

    ReplyDelete
  7. Thanx Mario, thanx a lot for your quick response and advice.

    Thanx a lot. Keep writing.

    ReplyDelete
  8. Nice tuts you have, keep on writing... regards from indonesia

    ReplyDelete
  9. I have just started experimenting with this interactive mode of PyQt. Under ipython, all works fine.

    However, under just python (2.7.5), the command line gets very sluggish. I have not found much else around about this and hence why I'm posting here.

    Have you found it sluggish? If so, is there something that can be done about it?

    Thanks!

    ReplyDelete
  10. @triccare Honestly, I don't have clue. I haven't been playing around with this for years so I can't tell if it sluggish or not and if there's any fixes.

    ReplyDelete