qhboxlayout举个例子
时间: 2023-08-09 14:05:02 浏览: 39
好的,以下是一个QHBoxLayout的例子:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
hbox = QHBoxLayout() # 创建一个 QHBoxLayout 对象
hbox.addWidget(okButton) # 将 okButton 控件添加到 QHBoxLayout 中
hbox.addWidget(cancelButton) # 将 cancelButton 控件添加到 QHBoxLayout 中
self.setLayout(hbox) # 将 QHBoxLayout 对象设置为窗口的布局管理器
self.setGeometry(300, 300, 300, 150) # 设置窗口的位置和大小
self.setWindowTitle('HBoxLayout') # 设置窗口的标题
self.show() # 显示窗口
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在这个例子中,我们创建了一个窗口,并在窗口中添加了两个按钮:一个是“OK”按钮,一个是“Cancel”按钮。我们使用QHBoxLayout来管理这两个按钮的位置和大小,将它们从左至右依次排列。然后,我们将QHBoxLayout对象设置为窗口的布局管理器,这样可以确保这两个按钮始终保持在正确的位置。最后,我们设置了窗口的位置和大小,并显示窗口。如果你运行这个例子,你将看到一个简单的窗口,其中有两个按钮。