In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of layout management in PyQt5. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
There is an important part of GUI programming that can not be ignored, and that is layout management. Layout management controls how our controls are placed in the application window. Layout management can be done in two ways. We can use absolute positioning or layout classes to control the position of controls in the program window.
Absolute positioning
Each control is placed at the location specified by the programmer. When you use absolute positioning, we need to understand the following limitations:
If we resize the window and the size and position of the controls remain the same
Applications may look different on different platforms
Changing the font may break the layout of the application
If we decide to change the layout, we must modify each control thoroughly, which is tedious and time-consuming
The following example is the absolute coordinate positioning of the control.
#! / usr/bin/python3#-*-coding: utf-8-*-import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLabelclass Example (QWidget): def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): lbl1 = QLabel ('my world you've been here', self) lbl1.move (15,10) lbl2 = QLabel ('CSND blog', self) lbl2.move (35,40) lbl3 = QLabel ('programmer') Self) lbl3.move (55,70) self.setGeometry (300,300,250,150) self.setWindowTitle ('absolute positioning') self.show () if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) ex = Example () sys.exit (app.exec_ ())
Tags (Label) are used in our examples. We locate them by providing x and y coordinate values. The origin of the coordinate system is the upper-left corner of the control. The value of x increases from left to right. The growth of y value is from top to bottom.
Lbl1 = QLabel ('my world you have been here', self) lbl1.move (15,10)
The label controls are placed in xboxes 15 and 10.
After the program is executed
Box layout box layout
Layout management uses layout classes in a more flexible and practical way. It is the preferred way to place a control in a window. QHBoxLayout and QVBoxLayout are the basic layout classes for horizontal and vertical alignment controls, respectively.
Imagine that we want to put two buttons in the lower right corner of the program. To create such a layout, we can use two boxes, one horizontal and one vertical. To create the necessary free space, we will add a stretch factor (stretch factor).
#! / usr/bin/python3#-*-coding: utf-8-*-"" PyQt5 tutorial in this example, we put two buttons in the lower right corner of the window. Author: my world you have been to the blog: http://blog.csdn.net/weiaitaowang final Editor: July 31, 2016 import sysfrom PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QVBoxLayout) QHBoxLayout) class Example (QWidget): def _ _ init__ (self): super (). _ _ init__ () self.initUI () def initUI (self): okButton = QPushButton ('OK') cancelButton = QPushButton ('cancel') hbox = QHBoxLayout () hbox.addStretch (1) hbox.addWidget (okButton) hbox.addWidget (cancelButton) vbox = QVBoxLayout () vbox.addStretch (1) vbox.addLayout (hbox) self.setLayout (vbox) self.setGeometry (300,300,350) Self.setWindowTitle ('Box layout') self.show () if _ _ name__ = ='_ main__': app = QApplication (sys.argv) ex = Example () sys.exit (app.exec_ ())
The example places two buttons in the lower-right corner of the window. When we resize the application window, they are fixed in the lower right corner. We use both HBoxLayout and QVBoxLayout layouts.
OkButton = QPushButton ('OK') cancelButton = QPushButton ('cancel')
Here we created two buttons.
Hbox = QHBoxLayout () hbox.addStretch (1) hbox.addWidget (okButton) hbox.addWidget (cancelButton)
We created a horizontal box layout, increasing the stretch factor (addStretch) and adding (addWidget) two buttons. A stretch factor is added before adding two buttons, which pushes the two buttons to the right of the window.
Vbox = QVBoxLayout () vbox.addStretch (1) vbox.addLayout (hbox)
To get the layout we want, we also need to put the horizontal layout into the vertical layout. The stretch factor on the vertical box pushes the controls inside the horizontal box to the bottom of the window.
Self.setLayout (vbox)
Finally, we set the main layout of the window.
After the program is executed
QGridLayout grid layout
The most frequently used layout class is grid layout. This layout divides the space into rows and columns. To create a grid layout, we use QGridLayout's class.
#! / usr/bin/python3#-*-coding: utf-8-*-"" PyQt5 tutorial in this example, we use a grid layout to create a calculator framework. Author: my world you have been to the blog: http://blog.csdn.net/weiaitaowang Last Editor: import sysfrom PyQt5.QtWidgets import (QApplication, QWidget,QPushButton, QGridLayout) class Example (QWidget): def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): grid = QGridLayout () self.setLayout (grid) names = ['Cls',' Bck',', 'Close',' 7' Positions = [(I, j) for i in range (5) for j in range (4)] for position, name in zip Names): if name =': continue button = QPushButton (name) grid.addWidget (button, * position) self.move (300,150) self.setWindowTitle ('Calculator') self.show () if _ name__ = ='_ main__': app = QApplication (sys.argv) ex = Example () sys.exit (app.exec_ ())
In our example, we put the button control we created in the grid.
Grid = QGridLayout () self.setLayout (grid)
Instantiate QGridLayout and set the layout of the application window.
Names = ['Cls',' Bck',', 'Close',' 7,'8,'9,'/','4,'5, 6, *,'1, 2, 3, -, 0, 4, 4, 5, 6, *, 1, 2, 3, -, 0, 5, +,]
This is the button label to be used in the future.
Positions = [(I, j) for i in range (5) for j in range (4)]
X We created a list of grid locations.
For position, name in zip (positions, names): if name = ='': continue button = QPushButton (name) grid.addWidget (button, * position)
Create buttons and add (addWidget) to the layout.
After the program is executed
Extended grid layout
Controls in a window can span multiple columns or rows in the grid. In the following example, we illustrate this point.
#! / usr/bin/python3#-*-coding: utf-8-*-"" PyQt5 tutorial in this example, we use GridLayout's cross-line to create a more complex window layout. Author: my world you have been to the blog: http://blog.csdn.net/weiaitaowang final Editor: July 31, 2016 import sysfrom PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QTextEdit, QLineEdit QGridLayout) class Example (QWidget): def _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): title = QLabel ('title') author = QLabel ('author') review = QLabel ('comment') titleEdit = QLineEdit () authorEdit = QLineEdit () reviewEdit = QTextEdit () grid = QGridLayout () grid.setSpacing (10) grid.addWidget (title, 1, 0) grid.addWidget (titleEdit, 1, 1) grid.addWidget (author, 2, 0) grid.addWidget (authorEdit, 2) 1) grid.addWidget (review, 3,0) grid.addWidget (reviewEdit, 3,1,5,1) self.setLayout (grid) self.setGeometry (300,300,350,300) self.setWindowTitle ('comments') self.show () if _ name__ = ='_ main__': app = QApplication (sys.argv) ex = Example () sys.exit (app.exec_ ())
The program we created contains three tags, two single-line text input boxes and a text editing control, using a QGridLayout layout.
Grid = QGridLayout () grid.setSpacing (10)
Instantiate the grid layout and set the spacing.
Grid.addWidget (reviewEdit, 3,1,5,1)
Add a control to the grid layout for which we can use row or column spans. In our example, we require the reviewEdit control to span five lines.
After the program is executed
Thank you for reading! This is the end of this article on "sample Analysis of layout Management in PyQt5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.