main_layout.addWidget(button_7, 1, 0) main_layout.addWidget(button_8, 1, 1) main_layout.addWidget(button_9, 1, 2) main_layout.addWidget(button_divide, 1, 3) main_layout.addWidget(button_4, 2, 0) main_layout.addWidget(button_5, 2, 1) main_layout.addWidget(button_6, 2, 2) main_layout.addWidget(button_multiply, 2, 3) main_layout.addWidget(button_1, 3, 0) main_layout.addWidget(button_2, 3, 1) main_layout.addWidget(button_3, 3, 2) main_layout.addWidget(button_subtract, 3, 3) main_layout.addWidget(button_0, 4, 0) main_layout.addWidget(button_clear, 4, 1, 1, 2) main_layout.addWidget(button_add, 4, 3) main_layout.addWidget(button_equal, 5, 0, 1, 4)
时间: 2024-04-13 10:27:13 浏览: 17
这段代码是使用PyQt库中的QGridLayout布局管理器来管理按钮的布局。代码中通过调用`addWidget()`方法来添加按钮到布局中,并指定按钮在布局中的位置。
具体来说,代码中使用`main_layout.addWidget()`方法来添加按钮对象到`main_layout`布局中。每个`addWidget()`方法都接受三个参数:按钮对象、按钮在布局中的行索引、按钮在布局中的列索引。
例如,`main_layout.addWidget(button_7, 1, 0)`将`button_7`按钮添加到布局中的第二行第一列。
通过不断调用`addWidget()`方法,可以将所有的按钮按照指定的行列位置添加到布局中,完成整个布局的搭建。最后,可以将布局设置给相应的窗口或部件,以实现界面的显示。
相关问题
button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button1.clicked.connect(self.show_query1_result) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button2.clicked.connect(self.show_query2_result) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button3.clicked.connect(self.show_query3_result) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button4.clicked.connect(self.show_query4_result) 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) 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.addStretch() button_layout.addWidget(button1) button_layout.addWidget(button2) button_layout.addWidget(button3) button_layout.addWidget(button4) button_layout.addWidget(button5) button_layout.addWidget(button6) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) # 将按钮布局添加到主窗口布局中 main_layout.addLayout(button_layout) # 创建两个窗口用于展示查询结果 self.query1_window = QueryResultWindow() self.query2_window = QueryResultWindow() self.query3_window = QueryResultWindow() self.query4_window = QueryResultWindow() self.query5_window = QueryResultWindow() self.query6_window = QueryResultWindow()怎么设置按钮位置为两列
要将按钮布局设置为两列,可以使用QGridLayout而不是QVBoxLayout。可以使用addWidget()方法将按钮添加到布局中,并指定它们的位置。以下是示例代码:
```
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)
```
在上面的代码中,`button_layout`是一个QGridLayout,使用addWidget()方法来添加按钮到布局中,并指定它们的行和列。`setColumnStretch()`方法用于添加一个空白列来填充布局,使其呈现为两列。
class MainWindow(QMainWindow): def init(self): super().init() # 设置主窗口大小 self.setFixedSize(800, 600) # 创建主窗口布局 main_layout = QVBoxLayout() central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) # 创建两个竖向按钮 button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.clicked.connect(self.show_query1_result) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.clicked.connect(self.show_query2_result) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.clicked.connect(self.show_query3_result) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.clicked.connect(self.show_query4_result) button_layout.addStretch() button_layout.addWidget(button1) button_layout.addWidget(button2) button_layout.addWidget(button3) button_layout.addWidget(button4) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) # 将按钮布局添加到主窗口布局中 main_layout.addLayout(button_layout) # 创建两个窗口用于展示查询结果 self.query1_window = QueryResultWindow() self.query2_window = QueryResultWindow() self.query3_window = QueryResultWindow() self.query4_window = QueryResultWindow() def show_query1_result(self): # 查询数据 db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return # 清空表格 self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels \ (["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) # 添加数据到表格 for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) # 显示窗口 self.query1_window.show()数据展示页面怎么设置筛选器按ID筛选结果并展示
你可以在数据展示页面添加一个 QLineEdit 和一个 QPushButton,让用户输入员工ID并点击按钮进行筛选。具体实现可以按照以下步骤进行:
1. 在数据展示页面的构造函数中创建 QLineEdit 和 QPushButton,并添加到布局中。
```python
self.filter_edit = QLineEdit()
self.filter_button = QPushButton('筛选')
self.filter_button.clicked.connect(self.filter_result)
button_layout.addWidget(self.filter_edit)
button_layout.addWidget(self.filter_button)
```
2. 创建一个方法 `filter_result` 用于筛选结果。该方法首先获取用户输入的员工ID,然后查询数据库获取符合条件的记录,最后将结果添加到表格中。
```python
def filter_result(self):
employee_id = self.filter_edit.text()
if not employee_id:
return
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = f"SELECT * FROM employee_salary WHERE employee_id={employee_id}"
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "无符合条件的记录")
return
self.table_widget.setRowCount(0)
for row_num, row_data in enumerate(result):
self.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
```
3. 将 QLineEdit 和 QPushButton 添加到布局中,并显示窗口即可。
完整代码如下:
```python
class QueryResultWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('查询结果')
self.setFixedSize(800, 600)
main_layout = QVBoxLayout()
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
table_widget = QTableWidget()
table_widget.setColumnCount(9)
table_widget.setHorizontalHeaderLabels(["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"])
main_layout.addWidget(table_widget)
button_layout = QHBoxLayout()
self.filter_edit = QLineEdit()
self.filter_button = QPushButton('筛选')
self.filter_button.clicked.connect(self.filter_result)
button_layout.addWidget(self.filter_edit)
button_layout.addWidget(self.filter_button)
button_layout.addStretch()
main_layout.addLayout(button_layout)
self.table_widget = table_widget
def filter_result(self):
employee_id = self.filter_edit.text()
if not employee_id:
return
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = f"SELECT * FROM employee_salary WHERE employee_id={employee_id}"
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "无符合条件的记录")
return
self.table_widget.setRowCount(0)
for row_num, row_data in enumerate(result):
self.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```