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!
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.
ReplyDeleteThere is also -wthread for wxPython and -gthread for GTK event loops.
You can also use stackless or greenlets to have background loops, it's also a tad more pythonic.
ReplyDeleteThis isn't particular to pyQt.
ReplyDeleteTake a look at iPython. You can do this with other gui frameworks as well.
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
ReplyDeleteI 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?
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.
ReplyDeleteI 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
Wish: I'm glad that you like my blog.
ReplyDeleteI 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.
Thanx Mario, thanx a lot for your quick response and advice.
ReplyDeleteThanx a lot. Keep writing.
Nice tuts you have, keep on writing... regards from indonesia
ReplyDeleteI have just started experimenting with this interactive mode of PyQt. Under ipython, all works fine.
ReplyDeleteHowever, 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!
@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.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete