<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # PyQt5 中的布局管理 > 原文: [http://zetcode.com/gui/pyqt5/layout/](http://zetcode.com/gui/pyqt5/layout/) 布局管理是我們將小部件放置在應用窗口中的方式。 我們可以使用絕對定位或布局類放置小部件。 使用布局管理器管理布局是組織窗口小部件的首選方式。 ## 絕對定位 程序員以像素為單位指定每個小部件的位置和大小。 使用絕對定位時,我們必須了解以下限制: * 如果我們調整窗口大小,則小部件的大小和位置不會改變 * 在各種平臺上,應用看起來可能有所不同 * 在我們的應用中更改字體可能會破壞布局 * 如果我們決定更改布局,則必須完全重做布局,這既繁瑣又耗時 以下示例將小部件放置在絕對坐標中。 `absolute.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows three labels on a window using absolute positioning. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ import sys from PyQt5.QtWidgets import QWidget, QLabel, QApplication class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): lbl1 = QLabel('Zetcode', self) lbl1.move(15, 10) lbl2 = QLabel('tutorials', self) lbl2.move(35, 40) lbl3 = QLabel('for programmers', self) lbl3.move(55, 70) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Absolute') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 我們使用`move()`方法定位小部件。 在我們的例子中,這些是標簽。 我們通過提供 x 和 y 坐標來定位它們。 坐標系的起點在左上角。 x 值從左到右增長。 y 值從上到下增長。 ```py lbl1 = QLabel('Zetcode', self) lbl1.move(15, 10) ``` 標簽窗口小部件位于`x=15`和`y=10`處。 ![Absolute positioning](https://img.kancloud.cn/87/aa/87aa3f0d128d939f25e17b255568d8bc_252x176.jpg) 圖:絕對定位 ## 盒子布局 `QHBoxLayout`和`QVBoxLayout`是在水平和垂直方向排列小部件的基本布局類。 想象一下,我們想在右下角放置兩個按鈕。 為了創建這樣的布局,我們使用一個水平框和一個垂直框。 為了創建必要的空間,我們添加了拉伸因子。 `buttons.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 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 2017 """ import sys from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): okButton = QPushButton("OK") cancelButton = QPushButton("Cancel") hbox = QHBoxLayout() hbox.addStretch(1) hbox.addWidget(okButton) hbox.addWidget(cancelButton) vbox = QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hbox) self.setLayout(vbox) self.setGeometry(300, 300, 300, 150) self.setWindowTitle('Buttons') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 該示例在窗口的右下角放置了兩個按鈕。 當我們調整應用窗口的大小時,它們會停留在該位置。 我們同時使用`HBoxLayout`和`QVBoxLayout`。 ```py okButton = QPushButton("OK") cancelButton = QPushButton("Cancel") ``` 在這里,我們創建兩個按鈕。 ```py hbox = QHBoxLayout() hbox.addStretch(1) hbox.addWidget(okButton) hbox.addWidget(cancelButton) ``` 我們創建一個水平框布局,并添加一個拉伸因子和兩個按鈕。 拉伸在兩個按鈕之前增加了可拉伸的空間。 這會將它們推到窗口的右側。 ```py vbox = QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hbox) ``` 將水平布局放置在垂直布局中。 垂直框中的拉伸因子會將帶有按鈕的水平框推到窗口底部。 ```py self.setLayout(vbox) ``` 最后,我們設置窗口的主要布局。 ![Buttons](https://img.kancloud.cn/2b/a2/2ba2eb26b0b4beebdf68a15f3b47995a_302x176.jpg) 圖:按鈕 ## `QGridLayout` `QGridLayout`是最通用的布局類。 它將空間分為行和列。 `calculator.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we create a skeleton of a calculator using QGridLayout. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ import sys from PyQt5.QtWidgets import (QWidget, QGridLayout, QPushButton, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): grid = QGridLayout() self.setLayout(grid) names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+'] positions = [(i,j) for i in range(5) for j in range(4)] for position, name in zip(positions, names): if name == '': continue button = QPushButton(name) grid.addWidget(button, *position) self.move(300, 150) self.setWindowTitle('Calculator') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在我們的示例中,我們創建了一個按鈕網格。 ```py grid = QGridLayout() self.setLayout(grid) ``` 創建`QGridLayout`的實例,并將其設置為應用窗口的布局。 ```py names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+'] ``` 這些是稍后用于按鈕的標簽。 ```py positions = [(i,j) for i in range(5) for j in range(4)] ``` 我們在網格中創建位置列表。 ```py for position, name in zip(positions, names): if name == '': continue button = QPushButton(name) grid.addWidget(button, *position) ``` 使用`addWidget()`方法創建按鈕并將其添加到布局。 ![Calculator skeleton](https://img.kancloud.cn/c8/2f/c82fd904206cd97e098cdc13d2c4888a_382x207.jpg) 圖:計算機骨架 ## 回顧示例 小部件可以跨越網格中的多個列或行。 在下一個示例中,我們將對此進行說明。 `review.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we create a bit more complicated window layout using the QGridLayout manager. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): title = QLabel('Title') author = QLabel('Author') review = QLabel('Review') titleEdit = QLineEdit() authorEdit = QLineEdit() reviewEdit = QTextEdit() grid = 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() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 我們創建一個窗口,其中有三個標簽,兩個行編輯和一個文本編輯小部件。 使用`QGridLayout`完成布局。 ```py grid = QGridLayout() grid.setSpacing(10) ``` 我們創建網格布局并設置小部件之間的間距。 ```py grid.addWidget(reviewEdit, 3, 1, 5, 1) ``` 如果我們將小部件添加到網格,則可以提供小部件的行跨度和列跨度。 在我們的例子中,我們使`reviewEdit`小部件跨越 5 行。 ![Review example](https://img.kancloud.cn/2f/cb/2fcb8d93e5c79db9f31f6070ce152c82_352x326.jpg) 圖:回顧 example PyQt5 教程的這一部分專門用于布局管理。
                  <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>

                              哎呀哎呀视频在线观看