Hello PyQt5, hellopyqt5
For GUI programming on ubuntu, PyQt5 is a good choice. First, install PyQt5. Terminal input command:Pip3 install PyQt5You can.
1. Create a directory x01.PyQtHello and add the file hello. py as follows:
import sysfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *app = QApplication(sys.argv)dlg = QDialog()label = QLabel("<font color=red size=5><b>Hello PyQt5!</b></font>")layout = QVBoxLayout()layout.addWidget(label)dlg.setLayout(layout)dlg.show()app.exec_()
After saving the file, enter the following command on the terminal:Python3 hello. pyRun. As follows:
2. Add another file calc. py as follows:
from __future__ import divisionimport sys from math import *from PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) self.browser = QTextBrowser() self.lineEdit = QLineEdit("Type an expression and press enter.") self.lineEdit.selectAll() layout = QVBoxLayout() layout.addWidget(self.browser) layout.addWidget(self.lineEdit) self.setLayout(layout) self.lineEdit.setFocus() self.lineEdit.returnPressed.connect(self.updateUi) self.setWindowTitle("Calculate") def updateUi(self): text = self.lineEdit.text() try: self.browser.append("%s = <b>%s</b>" % (text, eval(text))) except: self.browser.append("<font color=red>%s is invalid!</font>" % text)app = QApplication(sys.argv)form = Form()form.show()app.exec_()
Similarly, enter the following command:Python3 calc. pyRun. As follows:
Two small experiments show that PyQt5 is good for GUI programming, so it is recommended.