pyqt5最上面布局放4个按钮
时间: 2023-11-17 14:08:11 浏览: 44
你可以使用QHBoxLayout或QVBoxLayout来实现这个布局。以下是一个示例代码:
``` python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建四个按钮
btn1 = QPushButton('Button 1', self)
btn2 = QPushButton('Button 2', self)
btn3 = QPushButton('Button 3', self)
btn4 = QPushButton('Button 4', self)
# 创建水平布局
hbox = QHBoxLayout()
# 将按钮添加到布局中
hbox.addWidget(btn1)
hbox.addWidget(btn2)
hbox.addWidget(btn3)
hbox.addWidget(btn4)
# 设置窗口的布局
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Four Buttons')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在此示例中,我们创建了四个QPushButton,并使用QHBoxLayout将它们添加到窗口的布局中。这将使四个按钮在窗口的顶部水平对齐。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)