qpushbutton设置垂直
时间: 2023-05-27 07:03:16 浏览: 317
布局:
QPushButton默认是按照水平布局排列的,可以使用setSizePolicy()函数来设置控件的垂直布局参数。另外,如果要将多个QPushButton按照垂直布局排列,可以将它们放在QVBoxLayout中。
示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
class Window(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle("Vertical Layout Example")
self.setGeometry(200, 200, 300, 200)
# 创建垂直布局和QWidget
layout = QVBoxLayout()
widget = QWidget()
# 创建3个QPushButton并添加到布局
button1 = QPushButton("Button 1")
button2 = QPushButton("Button 2")
button3 = QPushButton("Button 3")
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(button3)
# 设置控件的垂直布局策略
button1.setSizePolicy(QPushButton.Expanding, QPushButton.Expanding)
button2.setSizePolicy(QPushButton.Expanding, QPushButton.Expanding)
button3.setSizePolicy(QPushButton.Expanding, QPushButton.Expanding)
# 将QWidget设置为主窗口的中心控件
widget.setLayout(layout)
self.setCentralWidget(widget)
if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
app.exec_()
```
输出:

阅读全文
相关推荐


















