A 'Hello World'-program doesn't make too much sense in PyQT, since the main objective is to create graphical user interfaces (GUI's) for whatever functionalities with it. For something more than minimal you may look into this one:

PyQT-tutorial

== ==

Here is a 'Hello World' program from Roberto Alsina in two versions, both of them are fully commented. One of it trivial (related to QT), one of it fully functional. Both of them should run on any newer Python/PyQT installation.

The trivial one:

# We will be using things from the qt and sys modules 
import qt,sys

# Every Qt application needs a QApplication object. sys.argv are the command line
# arguments. QApplication needs them so it can handle options, like -help (try it) 
a=qt.QApplication(sys.argv)

# We create a button called w. It says "Hello World" and has no parent (None as 
# second argument).
# Not having a parent means it's a top-level window, so it's not "inside" anything.
# In other words: a window with a button as its whole content.
w=qt.QPushButton("Hello World",None)

# Show the button
w.show()

# Enter the application loop. That means the application will start running,
# and will start to interact with the user.
# Since there is nothing to interact with, no user-input can stop the application from 'running'.
# The fully functional one below doesn't have this limitation and uses the main concept of PyQT.
a.exec_loop()

The fully functional one:

import qt,sys 

a=qt.QApplication(sys.argv)

w=qt.QPushButton("Hello World",None)

# Here the differences to the trivial one above may be seen, using QT's signal and slot features:

# We connect the clicked() signal to the button's "close" slot (method).
# Whenever you click on the button, it emits the signal. Then every slot that is
# connected to that signal gets called. In this case, only w.close
w.connect(w,qt.SIGNAL("clicked()"),w.close)

# We make w the main widget of the application. That means that when w is closed
# The application exits. If we didn't do this, we would have to call a.quit()
# to make it end.
a.setMainWidget(w)

w.show()

a.exec_loop()

Have fun!

HelloWorld (last edited 2005-08-09 20:47:03 by UlrichHauser)