ZetCode PyQt4 tutorial layout management

來源:互聯網
上載者:User

標籤:

!/usr/bin/python -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial This example shows three labels on a windowusing absolute positioning. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):                # The label widget is positioned at x=15 and y=10.        lbl1 = QtGui.QLabel(‘ZetCode‘, self)        lbl1.move(15, 10)        lbl2 = QtGui.QLabel(‘tutorials‘, self)        lbl2.move(35, 40)                lbl3 = QtGui.QLabel(‘for programmers‘, self)        lbl3.move(55, 70)                        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle(‘Absolute‘)            self.show()        def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == ‘__main__‘:    main()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we position two pushbuttons in the bottom-right corner of the window. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):                # Here we create two push buttons.        okButton = QtGui.QPushButton("OK")        cancelButton = QtGui.QPushButton("Cancel")        # We create a horizontal box layout and add a stretch factor and both buttons. The stretch adds a stretchable space before the two buttons. This will push them to the right of the window.        hbox = QtGui.QHBoxLayout()        # Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.        # 添加一個彈簧,填充空間        hbox.addStretch(1)        hbox.addWidget(okButton)        hbox.addWidget(cancelButton)        # To create the necessary layout, we put a horizontal layout into a vertical one. The stretch factor in the vertical box will push the horizontal box with the buttons to the bottom of the window.        vbox = QtGui.QVBoxLayout()        # 添加一個彈簧,填充空間        vbox.addStretch(1)        vbox.addLayout(hbox)                # Finally, we set the main layout of the window.        self.setLayout(vbox)                    self.setGeometry(300, 300, 300, 150)        self.setWindowTitle(‘Buttons‘)            self.show()        def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == ‘__main__‘:    main()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt4 import QtGui"""ZetCode PyQt4 tutorial In this example, we create a skeletonof a calculator using a QtGui.QGridLayout.author: Jan Bodnarwebsite: zetcode.com last edited: July 2014"""class Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):                # The instance of a QtGui.QGridLayout is created and set to be the layout for the application window.        grid = QtGui.QGridLayout()        self.setLayout(grid)         # These are the labels used later for buttons.        names = [‘Cls‘, ‘Bck‘, ‘‘, ‘Close‘,                 ‘7‘, ‘8‘, ‘9‘, ‘/‘,                ‘4‘, ‘5‘, ‘6‘, ‘*‘,                 ‘1‘, ‘2‘, ‘3‘, ‘-‘,                ‘0‘, ‘.‘, ‘=‘, ‘+‘]                # We create a list of positions in the grid.        positions = [(i,j) for i in range(5) for j in range(4)]                # Buttons are created and added to the layout with the addWidget() method.        for position, name in zip(positions, names):                        if name == ‘‘:                continue            button = QtGui.QPushButton(name)            grid.addWidget(button, *position)                    self.move(300, 150)        self.setWindowTitle(‘Calculator‘)        self.show()        def main():    app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == ‘__main__‘:    main()--------------------------------------------------------------------------------#!/usr/bin/python# -*- coding: utf-8 -*-"""ZetCode PyQt4 tutorial In this example, we create a bitmore complicated window layout usingthe QtGui.QGridLayout manager. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGuiclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):                title = QtGui.QLabel(‘Title‘)        author = QtGui.QLabel(‘Author‘)        review = QtGui.QLabel(‘Review‘)        titleEdit = QtGui.QLineEdit()        authorEdit = QtGui.QLineEdit()        reviewEdit = QtGui.QTextEdit()        # We create a grid layout and set spacing between widgets.        grid = QtGui.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)        # If we add a widget to a grid, we can provide row span and column span of the widget. In our case, we make the reviewEdit widget span 5 rows.        grid.addWidget(reviewEdit, 3, 1, 5, 1)                self.setLayout(grid)                 self.setGeometry(300, 300, 350, 300)        self.setWindowTitle(‘Review‘)            self.show()        def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == ‘__main__‘:    main()

 

ZetCode PyQt4 tutorial layout management

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.