Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of PyQt5 layout Management

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of PyQt5 layout management, which is very detailed and has certain reference value. Friends who are interested must finish it!

A brief introduction to GUI layout management

There are two main methods for interface layout management in PyQt5, one is absolute positioning, the other is using layout manager. The layout mode of absolute positioning in Qt can not adapt to the change of windows, so Qt provides classes for layout management of interface components, which are used to manage interface components, can automatically arrange interface components in windows, and automatically update the size of interface components when the size of the window changes.

QLayout is the abstract base class of layout manager in Qt. By inheriting QLayout, layout manager with different functions and complementary functions is realized. The layout manager is not the interface component, but the positioning strategy of the interface component; components of any container type can specify the layout manager; components in the same layout manager management have the same parent component, and the parent-child relationship has been implicitly specified while setting the layout manager.

The addWidget of the Qt layout manager is used to insert controls into the layout manager, and addLayout is used to insert the child layout manager into the layout manager.

2. Frame layout 1. QHBoxLayout

QHBoxLayout provides a horizontal way of layout management for controls.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QSizePolicyclass MainWindow (QWidget): def _ _ init__ (self, parent=None): super (). _ init__ (parent) self.layout = QHBoxLayout () self.layout.setSpacing (20) button = QPushButton ("Button1") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding) QSizePolicy.Expanding) self.layout.addWidget (button) button = QPushButton ("Button2") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout.addWidget (button) self.setLayout (self.layout) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow () window.resize Window.show () sys.exit (app.exec_ ()) 2, QVBoxLayout

QVBoxLayout provides a vertical way of layout management for controls.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSizePolicyclass MainWindow (QWidget): def _ _ init__ (self, parent=None): super (). _ init__ (parent) self.layout = QVBoxLayout () self.layout.setSpacing (20) button = QPushButton ("Button1") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding) QSizePolicy.Expanding) self.layout.addWidget (button) button = QPushButton ("Button2") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout.addWidget (button) self.setLayout (self.layout) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow () window.resize Window.show () sys.exit (app.exec_ ()) 3, nested layout

Layout managers can nest each other to form a complex layout management style. Examples of nesting use of the QBoxLayout layout manager are as follows:

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QSizePolicyclass MainWindow (QWidget): def _ _ init__ (self, parent=None): super (). _ init__ (parent) self.layout = QVBoxLayout () self.layout.setSpacing (20) # first line button layout management hLayout1 = QHBoxLayout () button = QPushButton ("Button1") button.setMinimumSize (60 30) button.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding) hLayout1.addWidget (button) button = QPushButton ("Button2") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding) hLayout1.addWidget (button) # second row button layout management hLayout2 = QHBoxLayout () button = QPushButton ("Button1") button.setMinimumSize (60 30) button.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding) hLayout2.addWidget (button) button = QPushButton ("Button2") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding) QSizePolicy.Expanding) hLayout2.addWidget (button) # overall vertical layout management self.layout.addLayout (hLayout1) self.layout.addLayout (hLayout2) self.setLayout (self.layout) if _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow () window.resize (400,200) window.show () sys.exit (app.exec_ ()) 3. Grid layout

The QGridLayout layout manager manages interface components in a grid manner.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QSizePolicyclass MainWindow (QWidget): def _ init__ (self, parent=None): super (). _ init__ (parent) self.layout = QGridLayout () self.layout.setSpacing (5) button = QPushButton ("Button1") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding,QSizePolicy.Expanding) self.layout.addWidget (button, 0,0,1) 1) button = QPushButton ("Button2") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding,QSizePolicy.Expanding) self.layout.addWidget (button, 0,1,1,1) button = QPushButton ("Button3") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding,QSizePolicy.Expanding) self.layout.addWidget (button, 1,0,1) 1) button = QPushButton ("Button4") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding,QSizePolicy.Expanding) self.layout.addWidget (button, 1,1,1,1) button = QPushButton ("Button5") button.setMinimumSize (60,30) button.setSizePolicy (QSizePolicy.Expanding,QSizePolicy.Expanding) # column extension Locate in row 2 and column 1, occupying 1 row and 2 columns self.layout.addWidget (button, 2,0,1,2) self.setLayout (self.layout) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow () window.resize (400,200) window.show () sys.exit (app.exec_ ()) IV, form layout

The QFormLayout layout manager uses forms to manage interface components. The tags and components in the form correspond to each other and support nested use.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QVBoxLayout, QLineEditfrom PyQt5.QtCore import Qtclass MainWindow (QWidget): def _ _ init__ (self Parent=None): super (). _ init__ (parent) self.layout = QFormLayout () self.layout.setSpacing (20) self.layout.setLabelAlignment (Qt.AlignLeft) self.layout.setFormAlignment (Qt.AlignRight) nameEdit = QLineEdit () mailEdit = QLineEdit () vLayout = QVBoxLayout () vLayout.setSpacing (6) addrEdit1 = QLineEdit () AddrEdit2 = QLineEdit () vLayout.addWidget (addrEdit1) vLayout.addWidget (addrEdit2) self.layout.addRow ("Name:" NameEdit) self.layout.addRow ("Mail:", mailEdit) self.layout.addRow ("Address:", vLayout) self.setLayout (self.layout) self.setWindowTitle ("FTP") if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow window.resize (400,200) window.show () sys.exit (app.exec_ ())

Fifth, stack layout

All the components managed by the QStackedLayout stack layout manager are perpendicular to the screen, and only one interface component is displayed on the screen at a time, as long as the topmost interface component is displayed.

The characteristics of QStackedLayout stack layout manager are as follows:

A. the component is of the same size and is filled with the display area of the parent component

B. you cannot nest other layout managers directly

C, you can freely switch the components that need to be displayed.

D. only one interface component can be displayed at a time

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QStackedLayout, QVBoxLayout, QPushButtonclass MainWindow (QWidget): def _ _ init__ (self Parent=None): super (). _ init__ (parent) self.layout = QStackedLayout () self.layout.addWidget (QPushButton ("Button1")) self.layout.addWidget (QPushButton ("Button2")) self.layout.addWidget (QPushButton ("Button3")) self.layout.addWidget (QPushButton ("Button4") self.setLayout (self.layout) self.setWindowTitle ("Stack") Layout ") # set the top of the stack to display the second component self.layout.setCurrentIndex (2) if _ _ name__ =" _ main__ ": app = QApplication (sys.argv) window = MainWindow () window.resize 200) window.show () sys.exit (app.exec_ ())

The QStackedLayout stack layout manager cannot nest other layout managers directly, but other layout managers can be used indirectly through QWidget container components.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QStackedLayout, QVBoxLayout, QPushButtonclass MainWindow (QWidget): def _ _ init__ (self Parent=None): super (). _ init__ (parent) self.layout = QStackedLayout () self.layout.addWidget (QPushButton ("Button1")) # Container component widget = QWidget () vLayout = QVBoxLayout () vLayout.addWidget (QPushButton ("Button2")) vLayout.addWidget (QPushButton ("Button3") widget.setLayout (vLayout) Self.layout.addWidget (widget) self.layout.addWidget (QPushButton ("Button4")) self.setLayout (self.layout) self.setWindowTitle ("Stack Layout") # sets the top of the stack to display the first component self.layout.setCurrentIndex (1) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = MainWindow () window.resize Window.show () sys.exit (app.exec_ ()) VI. Splitter

QSplitter is a layout manager with striping (splitter handle), and you can set the broadband of striping through the setHandleWidth () function. QSplitter can specify the split direction through the setOrientation () function, and the sub-components are arranged in the specified direction in the loading order.

Import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QSplitterfrom PyQt5.QtCore import Qtclass MainWindow (QWidget): def _ _ init__ (self Parent=None): super (). _ init__ (parent) self.layout = QVBoxLayout () self.mainSplitter = QSplitter (self) self.layout.addWidget (self.mainSplitter) self.setLayout (self.layout) # horizontal line segmentation self.mainSplitter.setOrientation (Qt.Horizontal) rightSplitter = QSplitter (self) # vertical line segmentation rightSplitter. SetOrientation (Qt.Vertical) textEdit = QTextEdit () textEdit.setText ("Window2") rightSplitter.addWidget (textEdit) textEdit = QTextEdit () textEdit.setText ("Window3") rightSplitter.addWidget (textEdit) textEdit = QTextEdit () textEdit.setText ("Window1") self.mainSplitter.addWidget (textEdit) self.mainSplitter.addWidget (rightSplitter Cut ratio self.mainSplitter.setStretchFactor (0 1) self.mainSplitter.setStretchFactor (1,2) self.mainSplitter.show () self.setWindowTitle ("Splitter") if _ _ name__ = = "_ main__": app = QApplication (sys.argv) window = MainWindow () window.resize (400,200) window.show () sys.exit (app.exec_ ())

The above is all the contents of the article "sample Analysis of PyQt5 layout Management". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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: 204

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report