• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 41, 文章 - 8, 評論 - 8, 引用 - 0
            數據加載中……

            [Python][PyQt4]PyQt4的 layout 管理

            PyQt4的 layout 管理

            在編程中一個重要的事情就是 layout 的管理。layout 管理就是在窗口中布置 widget。管理有兩種方式。我們可以使用 絕對定位 或者 layout 類

            絕對定位

            程序員可以指定每個 widget 的位置和大小。當你使用絕對定位時,你需要理解一些事情。

            • 如果你改變了窗口的大小,widget 的大小和位置是不會改變的。
            • 在不同平臺上應用可能看起來不太一樣。
            • 在你的應用中改變字體會毀掉你的 layout。
            • 如果你決定改變你的 layout,那么你必須要重新設計你的 layout,這意味著麻煩及耗時。
            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This example shows three labels on a window
            using absolute positioning.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    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()
            

            我們簡單地調用 move() 方法來定位我們的 widget 。在我們這個情況下,這些 widget 是標簽。我們通過給定 x 和 y 坐標進行定位。坐標系統是從左上角開始。x 的值從左到右增加。y 則是從上到下。

            Box Layout

            通過 layout 類管理會更加靈活和實用。這是優先考慮的方法。基本的 layout 類是 QtGui.QHBoxLayoutQtGui.QVBoxLayout 。它們分別以水平和垂直方式對 widget 進行布局。

            假設我們想把兩個按鈕放置在右下角。為了創建這樣的 layout,我們可以使用一個水平的和垂直的盒子。為了創建必要的空間,我們將會增加一個 縮放比例

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we position two push
            buttons in the bottom-right corner
            of the window.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    okButton = QtGui.QPushButton("OK")
                    cancelButton = QtGui.QPushButton("Cancel")
                    hbox = QtGui.QHBoxLayout()
                    hbox.addStretch(1)
                    hbox.addWidget(okButton)
                    hbox.addWidget(cancelButton)
                    vbox = QtGui.QVBoxLayout()
                    vbox.addStretch(1)
                    vbox.addLayout(hbox)
                    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()
            

            這里在窗口的右下角放置了兩個按鈕。他們在窗口調整時,會保持處于那個位置。我們使用了 QtGui.HBoxLayoutQtGui.QVBoxLayout

            okButton = QtGui.QPushButton("OK")
            cancelButton = QtGui.QPushButton("Cancel")
            

            此處,我們創建了兩個按鈕。

            hbox = QtGui.QHBoxLayout()
            hbox.addStretch(1)
            hbox.addWidget(okButton)
            hbox.addWidget(cancelButton)
            

            我們創建了一個水平的 layout 。增加了縮放比例和按鈕。這個縮放在兩個按鈕前增加了合適的縮放空白。這就使得按鈕在窗口的右邊。

            vbox = QtGui.QVBoxLayout()
            vbox.addStretch(1)
            vbox.addLayout(hbox)
            

            為了創建必要的 layout,我們把水平的 layout 放置到垂直的 layout 中。而垂直盒子中的縮放因子使得水平的盒子處于窗口的底部。

            最后,我們設置了窗口的 layout 。

            QtGui.QGridLayout

            最通用的 layout 類是網格 layout 。這個 layout 把空間分為行和列。為了創建一個網格的 layout,我們可以使用 QtGui.QGridLayout 類。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we create a skeleton
            of a calculator using a QtGui.QGridLayout.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    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)
                    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()
            

            在我們的例子中,我們創建了一組按鈕,并以網格放置。為了填補一個空格,我們增加了一個 QtGui.QLable 的 widget 。

            grid = QtGui.QGridLayout()
            

            這里,我們創建了一個網格 layout 。

            if j == 2:
                grid.addWidget(QtGui.QLable(''), 0, 2)
            else: grid.addWidget(button, pos[j][0], pos[j][1])
            

            為了把一個 widget 增加到網格中,我們調用了 addWidget() 方法。參數是 widget,行和列的數字。

            再看例子

            在網格中,widget 可以跨多行或多列。下面的例子我們就將介紹這個。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we create a bit
            more complicated window layout using
            the QtGui.QGridLayout manager.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class 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()
                    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)
                    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()
            

            我們創建了一個窗口,其中有三個標簽,兩個行編輯區及一個文本編輯區。layout 由 QtGui.QGridLayout 創建。

            grid = QtGui.QGridLayout()
            grid.setSpacing(10)
            

            我們創建了網格 layout,并設置了 widget 間的間隔。

            grid.addWidget(reviewEdit, 3, 1, 5, 1)
            

            如果我們增加一個 widget 到網格中,我們可以提供行和列的跨度。我們使得 reviewEdit 的跨度為 5 。


            這個部分我們講述了 layout 管理。

            posted on 2012-02-05 10:04 mirguest 閱讀(4650) 評論(0)  編輯 收藏 引用 所屬分類: Python

            久久久久亚洲爆乳少妇无| 亚洲国产二区三区久久| 97精品依人久久久大香线蕉97| 久久婷婷五月综合国产尤物app| 久久夜色精品国产欧美乱| 国产精品久久国产精品99盘| 久久er国产精品免费观看8| 国内高清久久久久久| 久久综合久久综合久久| 色天使久久综合网天天| 97久久超碰国产精品2021| 老司机午夜网站国内精品久久久久久久久| 国内精品九九久久精品| 精品久久久久国产免费| 久久久婷婷五月亚洲97号色| 青青草国产97免久久费观看| 久久精品中文无码资源站| 免费精品久久久久久中文字幕| 99久久婷婷国产综合亚洲| 久久精品国产亚洲αv忘忧草| 91久久精品视频| 国产成人精品免费久久久久| 国产aⅴ激情无码久久| 无码人妻少妇久久中文字幕| 久久免费香蕉视频| 欧美久久综合性欧美| www.久久热.com| 久久99亚洲网美利坚合众国| 亚洲女久久久噜噜噜熟女| 久久伊人影视| 日韩亚洲国产综合久久久| 久久久久久亚洲精品无码| 久久精品国产99久久久香蕉| 久久国产亚洲精品麻豆| 久久久青草青青亚洲国产免观| 丰满少妇高潮惨叫久久久| 久久久噜噜噜久久中文福利| 2021久久国自产拍精品| 久久美女人爽女人爽| 精品久久人人妻人人做精品| 久久成人永久免费播放|