<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # PySide 中的布局管理 > 原文: [http://zetcode.com/gui/pysidetutorial/layoutmanagement/](http://zetcode.com/gui/pysidetutorial/layoutmanagement/) GUI 編程中的重要事項是布局管理。 布局管理是我們將小部件放置在窗口上的方式。 管理可以通過兩種方式完成。 我們可以使用絕對定位或布局類。 ## 絕對定位 程序員以像素為單位指定每個小部件的位置和大小。 使用絕對定位時,您必須了解幾件事。 * 如果我們調整窗口大小,則小部件的大小和位置不會改變 * 各種平臺上的應用看起來可能有所不同 * 在我們的應用中更改字體可能會破壞布局 * 如果我們決定更改布局,則必須完全重做布局,這既繁瑣又耗時 ```py #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PySide tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: August 2011 """ import sys from PySide import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): label1 = QtGui.QLabel('Zetcode', self) label1.move(15, 10) label2 = QtGui.QLabel('tutorials', self) label2.move(35, 40) label3 = QtGui.QLabel('for programmers', self) label3.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()`方法來定位小部件。 在我們的例子中,這些小部件是標簽。 我們通過提供 x 和 y 坐標來定位它們。 坐標系的起點在左上角。 x 值從左到右增長。 y 值從上到下增長。 ![Absolute positioning](https://img.kancloud.cn/02/8c/028c296d11828d2db2be4e2dd8456c15_258x178.jpg) 圖:絕對定位 ## 盒子布局 具有布局類的布局管理更加靈活和實用。 這是在窗口上放置小部件的首選方法。 基本布局類是`QtGui.QHBoxLayout`和`QtGui.QVBoxLayout`。 他們水平和垂直排列小部件。 想象一下,我們想在右下角放置兩個按鈕。 為了創建這樣的布局,我們將使用一個水平框和一個垂直框。 為了創建必要的空間,我們將添加一個`stretch factor`。 ```py #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PySide 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: August 2011 """ import sys from PySide 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.HBoxLayout`和`QtGui.QVBoxLayout`。 ```py okButton = QtGui.QPushButton("OK") cancelButton = QtGui.QPushButton("Cancel") ``` 在這里,我們創建兩個按鈕。 ```py hbox = QtGui.QHBoxLayout() hbox.addStretch(1) hbox.addWidget(okButton) hbox.addWidget(cancelButton) ``` 我們創建一個水平框布局。 添加一個拉伸因子和兩個按鈕。 拉伸在兩個按鈕之前增加了可拉伸的空間。 這會將它們推到窗口的右側。 ```py vbox = QtGui.QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hbox) ``` 為了創建必要的布局,我們將水平布局放入垂直布局。 垂直框中的拉伸因子會將帶有按鈕的水平框推到窗口底部。 ```py self.setLayout(vbox) ``` 最后,我們設置窗口的基本布局。 它是垂直框。 ![Buttons example](https://img.kancloud.cn/d7/61/d76126fa26998edabc4fff570c8f32cf_308x178.jpg) 圖:按鈕示例 ## 網格布局 PySide 中最通用的布局類是網格布局。 此布局將空間分為行和列。 要創建網格布局,我們使用`QtGui.QGridLayout`類。 ```py #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PySide tutorial In this example, we create a skeleton of a calculator using a QGridLayout. author: Jan Bodnar website: zetcode.com last edited: August 2011 """ import sys from PySide 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.QLabel`小部件。 ```py grid = QtGui.QGridLayout() ``` 在這里,我們創建一個網格布局。 ```py if j == 2: grid.addWidget(QtGui.QLabel(''), 0, 2) else: grid.addWidget(button, pos[j][0], pos[j][1]) ``` 要將小部件添加到網格,我們調用`addWidget()`方法。 參數是小部件,行和列號。 ![Calculator skeleton](https://img.kancloud.cn/83/da/83dac49a4d42e3839f24964beaaacf9a_388x209.jpg) 圖:計算機骨架 ## 回顧示例 小部件可以跨越網格中的多個列或行。 在下一個示例中,我們將對此進行說明。 ```py #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PySide tutorial In this example, we create a bit more complicated window layout using the QGridLayout manager. author: Jan Bodnar website: zetcode.com last edited: August 2011 """ import sys from PySide 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() ``` 我們創建一個窗口,其中有三個標簽,兩個行編輯和一個文本編輯小部件。 使用`QtGui.QGridLayout`完成布局。 ```py grid = QtGui.QGridLayout() grid.setSpacing(10) ``` 我們創建網格布局并設置小部件之間的間距。 ```py grid.addWidget(reviewEdit, 3, 1, 5, 1) ``` 如果我們將小部件添加到網格,則可以提供小部件的行跨度和列跨度。 在我們的例子中,我們使`reviewEdit`小部件跨越 5 行。 ![Review example](https://img.kancloud.cn/e1/9d/e19d38c2dfcdd1d80708f1bc914c3615_352x332.jpg) 圖:回顧 example PySide 教程的這一部分專門用于布局管理。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看