The purpose of this article is to use pyQt Design to create a graphical Python GUI. When a button is clicked, the text content of a TextLabel changes  

Use VS CODE as the compiler, and install PYQT Integration plug-in for graphical.  UI compiles, converts.ui into.py, and any other compiler compiles pyQt in the same way  

First, you need to understand PyQt Design, which is a very simple GUI interface generator that presents PyQt programs graphically for people like us who have no programming background.  

Drag a button from the left (pushButton) and a label (TextLabel)  

Also remember that on the right side of PyQt Design -- Select button -- Property editor -- QObject -- objectName, change the name of the button to pushButton_1 and the label name to TextLabel_1 for later calls.  

Then I need to attach a trigger command to pushButton_1 (signal/slot), which is simply an action binding, but I don't understand it myself, so I will write a more detailed

Click on the seventh graphical button in the upper left corner, as shown in the image, which takes you to the interface for adding slots and signals. Click on PushButton and drag out something that looks like a ground cable. A dialog box pops up.  Then click edit on the right and add a ButtonClick () event by clicking the green plus sign in the dialog box that pops up. Be sure to add parentheses or you won't be able to add it.  Click OK, select the buttonClick () you just added on the right, and click OK. You can see that the button connects two lines, and the pyQt Design configuration is complete.  

pyqt design
pyqt design

 

  • Save it as a dot UI file

Open up the.ui file folder in vscode and go to the.ui file you just saved, right click, and select pyqt:  Compile form (note that this option must be installed with the PYQT Integration plug-in), which converts the.ui file into a.py file.  As a style for your program  

Then we need to write a running file for the style program:  

The complete code looks like this:  

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication, QGraphicsScene, QGraphicsPixmapItem, QCheckBox, QLabel
from PyQt5.QtGui import QImage, QPixmap
import sys

from Ui_untitled import Ui_MainWindow

class mywindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(mywindow,self).__init__()
        self.setupUi(self)
        
#此处的buttonclick为刚刚设置的触发槽名字
    def buttonclick (self):
#此处textlabel_1为刚刚设置的TextLabel,setText为设置其文本内容
        self.textlabel_1.setText('你点到button啦')

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = mywindow()

    window.show()

    sys.exit(app.exec_())

For a quick explanation, the first from import from, or something like that, imports the library you want to call from Python. Don't worry about that, just copy it

Below ui_untitled. py is the name of the ui_Untitled. py file you just compiled from untitled. UI. Change the red word to your own program name

from Ui_untitled import Ui_MainWindow

The latter two sentences are important
The complete code looks like this:

#此处的buttonclick为刚刚设置的触发槽名字
    def buttonclick (self):
#此处textlabel_1为刚刚设置的TextLabel,setText为设置其文本内容
        self.textlabel_1.setText('你点到button啦')

def buttonclick (self):  

Def buttonclick is the name of the trigger command you just set buttonclick(), don't get confused.  

Self.textlabel_1.settext (' You clicked button ')  

Textlabel_1 is the objectName of the original label, and setText is the command to set this value.  

The last one is just the code that makes the program work, just copy it.  

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = mywindow()

    window.show()

    sys.exit(app.exec_())

After running, you can click the button to modify the label text

If you want to modify the text attributes, you simply change the  

Self.textlabel_1.settext (' You clicked button ')  

The setText directive is changed to change the color of the text. The commands in parentheses can be changed as required  

20211215 alan

www.gongyesheji.org