def add_data(self): # 创建一个新窗口,并设置其属性 new_window = QWidget(self) new_window.setWindowTitle("添加员工信息") new_window.setGeometry(200, 200, 400, 300) # 创建返回按钮并设置其属性 return_button = QPushButton("返回", new_window) return_button.setGeometry(10, 10, 50, 30) return_button.clicked.connect(new_window.close) self.input_box = QLineEdit() self.status_label.setText("输入数据,格式为:部门,姓名,性别,职位,工号,状态") self.v_layout = QVBoxLayout(new_window) self.v_layout.addWidget(self.input_box) add_button = QPushButton("添加", new_window) add_button.setGeometry(300, 10, 50, 30) add_button.clicked.connect(lambda: self.insert_data(new_window)) new_window.setLayout(self.v_layout) new_window.show() # self.hide() # 隐藏父窗口中如何让("输入数据,格式为:部门,姓名,性别,职位,工号,状态")在窗口正上面
时间: 2024-02-06 09:03:13 浏览: 58
PyQt5创建一个新窗口的实例
You can add a QLabel to display the text "输入数据,格式为:部门,姓名,性别,职位,工号,状态" at the top of the new window. Here is an updated version of the code:
```
def add_data(self):
# 创建一个新窗口,并设置其属性
new_window = QWidget(self)
new_window.setWindowTitle("添加员工信息")
new_window.setGeometry(200, 200, 400, 300)
# 创建返回按钮并设置其属性
return_button = QPushButton("返回", new_window)
return_button.setGeometry(10, 10, 50, 30)
return_button.clicked.connect(new_window.close)
# 创建一个 QLabel 显示输入提示信息
input_label = QLabel("输入数据,格式为:部门,姓名,性别,职位,工号,状态", new_window)
input_label.setGeometry(10, 50, 300, 30)
# 创建一个 QLineEdit 接收输入数据
self.input_box = QLineEdit(new_window)
self.input_box.setGeometry(10, 80, 380, 30)
# 创建添加按钮并设置其属性
add_button = QPushButton("添加", new_window)
add_button.setGeometry(300, 10, 50, 30)
add_button.clicked.connect(lambda: self.insert_data(new_window))
# 创建一个 QVBoxLayout 并将输入框和按钮添加到其中
self.v_layout = QVBoxLayout(new_window)
self.v_layout.addWidget(input_label)
self.v_layout.addWidget(self.input_box)
self.v_layout.addWidget(add_button)
new_window.setLayout(self.v_layout)
new_window.show()
# self.hide() # 隐藏父窗口
```
This code adds a QLabel with the text "输入数据,格式为:部门,姓名,性别,职位,工号,状态" at the top of the new window, and positions it at (10, 50) using `setGeometry()`. The QLineEdit is positioned below the QLabel at (10, 80), and the QVBoxLayout containing both widgets is created and displayed in the new window.
阅读全文