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.input_box.setPlaceholderText("输入数据,格式为:部门,姓名,性别,职位,工号,状态") 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() 纠错
时间: 2024-02-27 09:53:09 浏览: 18
在这段代码中,`self.v_layout`没有进行实例化,需要在创建新窗口时,先进行以下实例化操作:
```
self.v_layout = QVBoxLayout(new_window)
```
这样才能够正确的使用 `addWidget()` 方法将控件添加到布局中。
相关问题
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() # 隐藏父窗口中如何让("输入数据,格式为:部门,姓名,性别,职位,工号,状态")在窗口正上面
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.
def add_data(self): # 创建一个新窗口,并设置其属性 new_window = QWidget() new_window.setWindowTitle("添加员工信息") new_window.setGeometry(200, 200, 400, 300) self.input_box = QLineEdit() self.input_box.setPlaceholderText("输入数据,格式为:部门,姓名,性别,职位,工号,状态") self.v_layout.addWidget(self.input_box) # 获取输入框中的数据 data = self.input_box.text().split(",") if len(data) != 6: self.status_label.setText("输入格式错误!请按照 部门,姓名,性别,职位,工号,状态 的格式输入。") return # 执行 SQL 插入语句 sql = "INSERT INTO mytable (部门, 姓名, 性别, 职位, 工号, 状态) VALUES (?, ?, ?, ?, ?, ?)" try: self.cursor.execute(sql, data) self.cnxn.commit() self.query("SELECT * FROM Staff") self.status_label.setText("数据添加成功!") except: self.status_label.setText("数据添加失败!") new_window.setLayout(self.v_layout)在打开同时关闭已打开的父窗口
这段代码中,如果想要在打开同时关闭已打开的父窗口,可以在打开新窗口时传入父窗口参数,并在新窗口中创建一个返回按钮,点击该按钮时关闭当前窗口并返回到父窗口。具体实现方法可以参考如下代码:
```
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.input_box.setPlaceholderText("输入数据,格式为:部门,姓名,性别,职位,工号,状态")
self.v_layout.addWidget(self.input_box)
# 获取输入框中的数据
data = self.input_box.text().split(",")
if len(data) != 6:
self.status_label.setText("输入格式错误!请按照 部门,姓名,性别,职位,工号,状态 的格式输入。")
return
# 执行 SQL 插入语句
sql = "INSERT INTO mytable (部门, 姓名, 性别, 职位, 工号, 状态) VALUES (?, ?, ?, ?, ?, ?)"
try:
self.cursor.execute(sql, data)
self.cnxn.commit()
self.query("SELECT * FROM Staff")
self.status_label.setText("数据添加成功!")
except:
self.status_label.setText("数据添加失败!")
new_window.setLayout(self.v_layout)
new_window.show()
self.hide() # 隐藏父窗口
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)