Learning pyqt basics 3

Source: Internet
Author: User
Layout manager in pyqt4

Layout management is the method for us to arrange the location in the window. There are two ways to manage layout: absolute positioning and layout category. In general, the absolute layout method is rarely used, just like Web pages. Currently, there are no popular responsive la S.

Layout classes)

The layout manager in layout category mode is more flexible and practical than the layout manager in absolute positioning mode. It is the first layout management method for window components. The most basic layout types are qhboxlayout and qvboxlayout.

class Boxlayout(QtGui.QWidget):    def __init__(self, parent = None):        QtGui.QWidget.__init__(self)        self.setWindowTitle(‘box layout‘)        ok = QtGui.QPushButton(‘OK‘)        cancel = QtGui.QPushButton(‘Cancel‘)        hbox = QtGui.QHBoxLayout()        hbox.addStretch(1)        hbox.addWidget(ok)        hbox.addWidget(cancel)        vbox = QtGui.QVBoxLayout()        vbox.addStretch(1)        vbox.addLayout(hbox)        self.setLayout(vbox)        self.resize(300, 150)
  • The code is easy to understand. As for the vbox. addstretch () method, add a scaling interval element (for more information, see the document)
Grid layout
class GridLayout(QtGui.QWidget):    def __init__(self, parent = None):        QtGui.QWidget.__init__(self)        self.setWindowTitle(‘grid layout‘)        names = [‘Cls‘, ‘Bck‘, ‘‘, ‘Close‘, ‘7‘, ‘8‘, ‘9‘, ‘/‘,        ‘4‘, ‘5‘, ‘6‘, ‘*‘, ‘1‘, ‘2‘, ‘3‘,        ‘-‘, ‘0‘, ‘.‘, ‘=‘, ‘+‘]    grid = QtGui.QGridLayout()    j = 0    pos = [(0, 0), (0, 1), (0, 2), (0, 3),    (1, 0), (1, 1), (1, 2), (1, 3),    (2, 0), (2, 1), (2, 2), (2, 3),    (3, 0), (3, 1), (3, 2), (3, 3),    (4, 0), (4, 1), (4, 2), (4, 3)]    for i in names:        button = QtGui.QPushButton(i)        if j == 2:            grid.addWidget(QtGui.QLabel(‘‘), 0, 2)        else:            grid.addWidget(button, pos[j][0], pos[j][1])        j = j + 1    self.setLayout(grid)
  • I feel that the code is still quite understandable, and I didn't talk much about it in the book...

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.