PyQt
API is GUI widgets toolkit in python. QtCore
module contains non-GUI function and QtGui
contains the graphical controls.
QWidget
class is derived from QObject
and QPaintDevice
classes. it is the base class for user interface objects.
QMainWindow
is derived from QWidget
. Most GUI window is created by QMainWindow
.
A first example of PyQt to show the basic structure:
import sys
from PyQt4 import QtGui, QtCore
class myApp(QtGui.QWidget):
def __init__(self):
super(myApp, self).__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("PyQt example")
self.home()
# self.show()
def home(self):
#1 add a button
#1.1 button has clicked signal and connect to a slot function
self.btn1 = QtGui.QPushButton("Quit", self)
self.btn1.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.btn1.move(100, 100)
self.btn2 = QtGui.QPushButton("Print Test", self)
self.btn2.clicked.connect(self.print_test)
self.btn2.move(200, 100)
self.label = QtGui.QLabel(self)
self.label.setText("This is old label")
self.label.move(200, 50)
def print_test(self):
self.label.setText("Button clicked!")
def main():
app = QtGui.QApplication(sys.argv)
gui = myApp()
gui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
A good way to organize the code is following Eli Bendersky first example and Eli Bendersky second example
Reference 1. http://www.tutorialspoint.com/pyqt/pyqt_hello_world.htm 2. https://pythonprogramming.net/basic-gui-pyqt-tutorial/