The basic operation of the form of the development of Python Qt

This article uses Qt4, is a python (x, y) suite of integrated, why not integrate Qt5 it, too lazy to install ah :)


text:


First look at the finished product:

13717038-a538023465bf0495.png

The function of this program is to enter the original price and percentage of price, calculated the final price.


Designer section

Then is a development stage, first create a form in Qt Designer, the picture probably looks like, and then dragged into the controls

13717038-c0143a41dddb46ac.png


Among them, "the original price how much ah", "green label marked" These kind of control is the text QLabel , the first frame is QTextEdit , the second standard standard is QDoubleSpinBox , or use QSpinBox all, the two main types of digital s difference,

Submit button is QPushButton , here mainly want to try different controls, QLabel Next, click on the picture box, and then edit the properties will be in the name objectName labelResult  , QTextEdit named textPrice, QDoubleSpinBox

Named dSBPer  , the QPushButton named btnSub FIG:

13717038-f848d1bd1d6e35e6.png
13717038-5fe15d9b568d3214.png
13717038-c177dece7b3727d6.png
13717038-36530a3b90420799.png


This naming and no special meaning, so that I can just remember some of the more firmly, to prevent the program hit half back and forth to see the designer.


Here is a troublesome thing ------------ picture frame

Pictures show itself is QLabel, so you need to display pictures placed in the designer a QLbel control, change control does not change in the program, so the default name like, the next need to add the resource file in Explorer, as shown:


13717038-fd23606f7b67c81f.png


In Qt Designer, you can simply load the pixmap property QLabel controls, support files accessible Qt documentation, I'll post later in the article


It is worth noting that the resource file in the program needs to be compiled for the .py file

Compiled code as follows:

pyrcc4 -o a1_rc.py a1.qrc


Part of the program

Start now part of the editing program

The first step is to import about Qt library


13717038-f60f601da14648dd.png

Then create initialization Window


13717038-49fdd75f1be46bc6.png

Next is the basis for the preparation of class


13717038-6e6c76f904758274.png

Where "self.btnSub.clicked.connect (self.CalculateTax)" in, btnSub is the name of the button, clicked.connect is connected to the function button click event to be executed, CalculateTax my own functions written in clicks, he will will be called after the button is clicked


13717038-e8b769435cd6dde7.png

This line main program is a program execution operation of the form

Then he began to write CalculateTax (self) function, and place it in App class:


13717038-d0001d9521fa89f7.png

"TextPrice.toPlainText ()" which is the name of the control textPrice, toPlainText () is a method of control, which is to get control of text, function specific detailed information can be queried in the Qt documentation, empathy behind the controls

Qt is little, if garbled Chinese, the Chinese need to specifying the encoding, if necessary to see the specific coding program, the code was added in this direct u string before transcoding.


13717038-a75e142db2212011.png

Attached hereto is the official Qt documentation page:

http://doc.qt.io/qt-5/classes.html

Reproduced in: https: //www.jianshu.com/p/2d79399d46d0

Guess you like

Origin blog.csdn.net/weixin_34319374/article/details/91072604