Hello World
這個基本程式碼將使用 PyQt4 啟動一個 Hello world
GUI 視窗:
import sys
from PyQt4 import QtGui
# create instance of QApplication
app = QtGui.QApplication(sys.argv)
# create QLabel, without parent it will be shown as window
label = QtGui.QLabel('Hello world!')
label.show()
# start the execution loop of the application
sys.exit(app.exec_())
這是使用 PyQt5 的相同程式碼。
import sys
from PyQt5 import QtWidgets
# create instance of QApplication
app = QtWidgets.QApplication(sys.argv)
# create QLabel, without parent it will be shown as window
label = QtWidgets.QLabel('Hello world!')
label.show()
# start the execution loop of the application
sys.exit(app.exec_())