Basic operations for form development of Python QT

Source: Internet
Author: User
Tags qt designer

This article uses the QT4, is the python (x, y) Suite integrates, why does not integrate QT5, does not bother to install AH:)

Body:

First look at the finished product:

The function of this program is to enter the original price and the percentage of the price reduction to calculate the final prices.

    • Designer section

Then there is the development phase, where you first create a form in Qt designer, presumably like the picture, and then drag the controls in

Among them, "What is the original price is Ah", "green Label" The text class of the control is Qlabel, the first box is Qtextedit, the second label is Qdoublespinbox, Or the use of Qspinbox, the two are mainly different types of numbers,

Submit button is Qpushbutton, here is to try a different control, next click on the picture box Qlabel, and then in the property editor named ObjectName Labelresult , Qtextedit named Textprice,qdoublespinbox

named Dsbper ,Qpushbutton named Btnsub

There is no special meaning to this naming method, but it can make me remember a little bit more, to prevent the program from hitting half way back and forth looking at the designer.

There's a lot of trouble here.------------Picture Frame

The picture display itself is Qlabel, so the display picture needs to first put a Qlbel control in the designer, change the control in the program is not changed, so the name is good by default, next also need to add the resource file in the Explorer,

In the QT designer, just load in the Pixmap property of the Qlabel control, and the supported files can access the QT documentation, which I'll post later in the article

It is worth noting that the use of resource files in a program needs to be compiled into a. py file

The compiled code is as follows:

Pyrcc4-o a1_rc.py A1.QRC

    • Program section

Now we're going to start editing the program section.

The first step is to first import a library about QT

Import SYS  from Import Qtcore,qtgui,uic

Then create a window initialization

" "  Window File= Uic.loaduitype (qtcreatorfile)

Next is the writing of the base class

class MyApp (Qtgui.qmainwindow,ui_mainwindow):     def __init__ (self):        Qtgui.qmainwindow. __init__ (self)        Ui_mainwindow. __init__ Self.setupui (self),         self.btnSub.clicked.connect (self). Calculatetax)

where "self.btnSub.clicked.connect" (self. Calculatetax)", Btnsub is the name of the button, Clicked.connect is the button's click event to perform the function of the connection, Calculatetax is my own written click function, he will be clicked after the button is called

if __name__ " __main__ " :     = qtgui.qapplication (sys.argv)    = App ()    window.show ()    sys.exit (App.exec_ ())    

This line is in the main program, which shows the action of the form when the program executes.

The next step is to write the calculatetax (self) function, which is placed in the app class:

 def   Calculatetax (self): Price  = int (Self.textPrice.toPlainText ()) tax  = float (SELF.DSBP Er.value ()) Total_price  = price-((tax/100) *price) total_price_string = u "  You have to remember how much you're going to spend, the price you expect is cheap:   " +str (total_price) +u"   Block   +"   \ n   +u"   See, not yet, buy   "  Self.labelResult.setText (total_price_string)  

"Textprice.toplaintext ()" where Textprice is the name of the control, Toplaintext () is the control's method, which is to get the text content of the control, the details of the function can be queried in the Qt document, and the subsequent control is the same

One point is in Qt, if the Chinese display garbled, the need to change the Chinese language to the specified encoding, the specific situation needs to see the code of the program, in the text before the string plus u can be directly transcoded.

#-*-coding:utf-8-*-ImportSYS fromPyQt4ImportQtcore,qtgui,uicqtcreatorfile="Untitled.ui" #Window FileUi_mainwindow,qtbaseclass=Uic.loaduitype (qtcreatorfile)classApp (Qtgui.qmainwindow,ui_mainwindow):def __init__(self): Qtgui.qmainwindow.__init__(self) ui_mainwindow.__init__Self.setupui (self), self.btnSub.clicked.connect (self). Calculatetax)defCalculatetax (self): price=Int (Self.textPrice.toPlainText ()) tax=Float (self.dSBPer.value ()) Total_price= price-((tax/100) *Price ) total_price_string= u"you have to remember how much you are going to spend, and the expected price is very cheap:"+str (Total_price) +u"Block"+"\ n"+u"See, I can't buy it."Self.labelResult.setText (total_price_string)if __name__=="__main__": App=qtgui.qapplication (sys.argv) window=App () window.show () Sys.exit (App.exec_ ())
All Code

The Official QT Documentation page is attached to the following:

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

And then it's gone.

Basic operations for form development of Python QT

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.