# 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() }}}
#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()
import sys;
from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import *;
from PyQt4.QtGui import *;
class MainWindow(QDialog):
def __init__(self, parent=None):
#QMainWindow.__init__(self, parent)
#self.setWindowTitle(self.tr("Hello World From PyQt!"))
btnpush.connect(btnpush, SIGNAL("clicked()"), btnpush.close)
if __name__ == "__main__":
app = QApplication(sys.argv)
btnpush = QPushButton("Hello World", None)
btnpush.show()
#mainwindow = MainWindow()
#mainwindow.show()
sys.exit(app.exec_())
【
import sys;
from PyQt4.QtCore import *;
from PyQt4.QtGui import *;
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle(self.tr("Hello World From PyQt!"))
if __name__ == "__main__":
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
】