button_layout = QGridLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button1.clicked.connect(self.show_query1_result) button_layout.addWidget(button1, 0, 0) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button2.clicked.connect(self.show_query2_result) button_layout.addWidget(button2, 0, 1) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button3.clicked.connect(self.show_query3_result) button_layout.addWidget(button3, 1, 0) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button4.clicked.connect(self.show_query4_result) button_layout.addWidget(button4, 1, 1) button5 = QPushButton('按员工ID查询员工工资') button5.setFixedSize(200, 50) button5.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button5.clicked.connect(self.show_query5_result) button_layout.addWidget(button5, 2, 0) button6 = QPushButton('按员工ID查询鸡爪领取记录') button6.setFixedSize(200, 50) button6.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button6.clicked.connect(self.show_query6_result) button_layout.addWidget(button6, 2, 1) button_layout.setColumnStretch(2, 1) # 添加空白列来填充布局 layout = QHBoxLayout() layout.addLayout(button_layout) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget)居中排列
时间: 2024-02-01 07:16:16 浏览: 135
你可以将上述代码中的布局添加到一个 QVBoxLayout 中,然后将 QVBoxLayout 设置为 QWidget 的布局,再将 QWidget 作为 QMainWindow 的 central widget,即可实现居中排列。示例代码如下:
```python
button_layout = QGridLayout()
button1 = QPushButton('当日员工工资')
button1.setFixedSize(200, 50)
button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button1.clicked.connect(self.show_query1_result)
button_layout.addWidget(button1, 0, 0)
button2 = QPushButton('当日鸡爪领取记录')
button2.setFixedSize(200, 50)
button2.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button2.clicked.connect(self.show_query2_result)
button_layout.addWidget(button2, 0, 1)
button3 = QPushButton('查询历史员工工资')
button3.setFixedSize(200, 50)
button3.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button3.clicked.connect(self.show_query3_result)
button_layout.addWidget(button3, 1, 0)
button4 = QPushButton('查询历史鸡爪领取记录')
button4.setFixedSize(200, 50)
button4.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button4.clicked.connect(self.show_query4_result)
button_layout.addWidget(button4, 1, 1)
button5 = QPushButton('按员工ID查询员工工资')
button5.setFixedSize(200, 50)
button5.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button5.clicked.connect(self.show_query5_result)
button_layout.addWidget(button5, 2, 0)
button6 = QPushButton('按员工ID查询鸡爪领取记录')
button6.setFixedSize(200, 50)
button6.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button6.clicked.connect(self.show_query6_result)
button_layout.addWidget(button6, 2, 1)
button_layout.setColumnStretch(2, 1) # 添加空白列来填充布局
v_layout = QVBoxLayout()
v_layout.addStretch() # 添加弹性空间,使按钮布局垂直居中
v_layout.addLayout(button_layout)
v_layout.addStretch() # 再添加一次弹性空间
widget = QWidget()
widget.setLayout(v_layout)
self.setCentralWidget(widget)
```
阅读全文