• <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

            人人狠狠综合88综合久久| 成人国内精品久久久久一区| 99久久久精品免费观看国产| 88久久精品无码一区二区毛片| 久久久久亚洲AV成人网人人网站 | 久久久久久久人妻无码中文字幕爆| 狠狠88综合久久久久综合网 | 一本一本久久a久久综合精品蜜桃| 久久亚洲精品无码AV红樱桃| 亚洲国产精品久久久久网站| 18禁黄久久久AAA片| 26uuu久久五月天| 久久综合狠狠综合久久| 久久影院午夜理论片无码| 久久国产亚洲精品无码| 漂亮人妻被中出中文字幕久久| 欧美久久精品一级c片片| 久久久久av无码免费网| 91麻豆精品国产91久久久久久| 性欧美大战久久久久久久久| 国产三级精品久久| 久久精品国产亚洲AV无码麻豆| 亚洲欧洲中文日韩久久AV乱码| 久久99国产精品99久久| 久久精品水蜜桃av综合天堂| 精品久久亚洲中文无码| 免费一级欧美大片久久网 | 久久精品国产亚洲av麻豆蜜芽| 久久国产视屏| 久久久精品久久久久特色影视| 久久91精品国产91久久小草| 色88久久久久高潮综合影院| 亚洲国产精品无码久久一线| 久久久无码精品亚洲日韩京东传媒 | 国产精品久久久久影院嫩草 | 久久久久久久综合狠狠综合| 久久国产高清一区二区三区| 国产激情久久久久影院| Xx性欧美肥妇精品久久久久久 | 久久久精品国产sm调教网站 | 午夜视频久久久久一区|