PYQT5 every day must learn the layout management

Source: Internet
Author: User
This article is mainly for everyone in detail on the PYQT5 every day must learn the layout of the relevant information, with a certain reference value, interested in small partners can refer to

There is a part of GUI programming that cannot be neglected, that is, layout management. Layout management controls how our control is placed in the application window. There are two ways in which layout management can be done. We can use the absolute positioning or layout class two methods to program the control position in the window.

Absolute positioning

Each control is placed at the location specified by the programmer. When you use absolute positioning, we want to understand the following limitations:

    • If we resize the window the size and position of the control remains the same

    • Applications may look different on different platforms

    • Changing the font may break the layout of your application

    • If we decide to change the layout, we have to modify each control completely, which is tedious and time-consuming

The following example is the absolute coordinate positioning method of the control.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 Tutorial This example shows the three tags that use absolute positioning in a window. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang last edited: July 31, 2016 "" "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 ever been to ', self)  lbl1.move (at)  lbl2 = Qlabel (' csnd blog ', self)  Lbl2.move (+)  lbl3 = Qlabel (' Programmer ', self)  lbl3.move (+)  self.setgeometry (  +, +) Self.setwindowtitle (' absolute positioning ')    self.show () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () Sys.exit (App.exec_ ())

The label is used in our example. We position them by providing x and Y coordinate values. The original point of the coordinate system is the upper-left corner of the control. The X-value growth is left-to-right. The Y-value growth is from top to bottom.

LBL1 = Qlabel (' My World you've been here ', self) lbl1.move (15, 10)

Label controls are placed in X=15 and y=10.

After the program executes

Box layout

Layout management uses layout classes in a more flexible and practical way. It is the preferred way to put a control in a window. Qhboxlayout and Qvboxlayout are 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 a horizontal vertical two box. To create the necessary free space, we will add a tensile factor (stretch factor).


#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 tutorial in this example, we place two buttons in the lower-right corner of the window. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang last edited: 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 ()  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 also use the hboxlayout and qvboxlayout layouts.

OKButton = Qpushbutton (' OK ') CancelButton = Qpushbutton (' Cancel ')

Here we have created two buttons.

Hbox = Qhboxlayout () hbox.addstretch (1) hbox.addwidget (okbutton) hbox.addwidget (CancelButton)

We created a horizontal box layout that adds a stretch factor (Addstretch) and adds (AddWidget) two buttons. A stretch factor is added before two buttons are added, which pushes two buttons to the right of the window.

VBox = Qvboxlayout () vbox.addstretch (1) vbox.addlayout (Hbox)

To get the layout we want, you need to put the landscape layout in a vertical layout. The stretch factor on the vertical box pushes the control 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 executes

Qgridlayout Grid Layout

The most frequently used layout class is the grid layout. This layout divides the space into rows and columns. To create a grid layout, we use the Qgridlayout class.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 tutorial in this example, we use a grid layout to create a calculator frame. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang last edited: July 31, 2016 "" "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 ', ' 8 ', ' 9 ', '/',  ' 4 ', ' 5 ', ' 6 ', ' * ', ' 1 ', ' 2 ', ' 3 ', '  -',  ' 0 ', '. ', ' = ', ' + ', ' = ', ' + ', '  positions = [ (I, J) for I in range (5) for J in Range (4)]  for position, name in zip (positions, names):   if Name = = ":    con Tinue   button = Qpushbutton (name)   grid.addwidget (button, *position)  self.move (.)  Self.setwindowtitle (' calculator ')    self.show () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () Sys.exit (App.exec_ ())

In our example, we'll put the button controls we created in the grid.

Grid = Qgridlayout ()  self.setlayout (GRID)

Instantiate the Qgridlayout and set the layout of the application window.

names = [' Cls ', ' Bck ', ' ', ' Close ', '   7 ', ' 8 ', ' 9 ', '/', '  4 ', ' 5 ', ' 6 ', ' * ', ' 1 ', ' 2 ', ' 3 ', '  -',  ' 0 ', '. ', '=', '+',]

This is the button label that will be used later.

positions = [(I, J) for I in range (5) for J in Range (4)]

X we have created a list of grid locations.

For position, name in zip (positions, names):   if Name = = ":    continue   button = Qpushbutton (name)   Grid.addwidget (button, *position)

Create a button and add (AddWidget) to the layout.

After the program executes

Extend Grid layout

A control in a window can span multiple columns or rows in a grid. In the following example, we illustrate this point.


#!/usr/bin/python3#-*-coding:utf-8-*-"" "PYQT5 tutorial in this example, we created a more complex window layout using the GridLayout cross-line. Author: My World You've been to the blog: Http://blog.csdn.net/weiaitaowang last edited: 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)  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 (+, +)  Self.setwindowtitle (' comment ')    self.show () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () Sys.exit (App.exec_ ())

The program we created contains three labels, two single-line text input boxes and a text-editing control, using the Qgridlayout layout.

Grid =qgridlayout () grid.setspacing (10)

Instantiates the grid layout and sets the set spacing.

Grid.addwidget (Reviewedit, 3, 1, 5, 1)

Add a control to the grid layout, and we can use a row or column span for the control. In our example, we require that the Reviewedit control span 5 rows.

After the program executes

This section of the PYQT5 tutorial specifically describes layout management. The event-related content of PYQT5 will be described later.

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.