The centering and layout of pyqt5 forms and the use of library Qlabel
This article introduces the knowledge of "the center and layout of pyqt5 forms and the usage of library Qlabel". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Second, the center of the form
1. By default, our form is centered on the screen by default.
two。 Manually adjust to the center of the screen
Get the object QDesktopWidget of the desktop control through the desktop () function
Get the resolution of the width of the screen through the width () function of the desktop object
For example, output desktop resolution:
Dk=app.desktop ()
Print (dk.width ())
Form schematic diagram
Horizontal center
Dk.width () / 2-mf1.width () / 2
Vertical center
Dk.height () / 2-mf1.height () / 2
Third, learn a new control
The use of label text controls requires us to use the class library Qlabel
Use format:
Variable object = QLabel (the text content of the label, the control in which the label is located)
Examples of use:
Lbl=QLabel ("programming creates cities", self)
Use of absolute layout:
Directly through the move to the location of a pixel
Features: very flexible
Weakness: cannot change with the form
IV. Summary and emphasis
1. The method of mastering the central layout of the form
two。 Master the method of using absolute layout and its advantages and disadvantages
The knowledge source code of this section:
Import sysfrom PyQt5.QtWidgets import QApplication,QWidget,QLabel
Class MyForm1 (QWidget): def _ init__ (self): super (). _ init__ () self.initUI ()
Def initUI (self): self.setWindowTitle ("Liu Jinyu programming") lbl=QLabel ("programming creates the City", self) self.resize (400300) self.move (50jing50) self.show ()
Def center (self): pass
If _ name__== "_ main__": app=QApplication (sys.argv) dk = app.desktop () # initialized form mf1=MyForm1 () mf1.move (dk.width () / 2-mf1.width () / 2, dk.height () / 2-mf1.height () / 2)
That's all for app.exec_ (), "pyqt5 form centering and layout and library Qlabel usage". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!