class NextWindow(QMainWindow): def init(self, user_id): super().init() self.user_id = user_id self.initUI() # 创建串口线程 self.serial_thread = SerialThread() self.serial_thread.data_received.connect(self.handle_data) self.serial_thread_thread = threading.Thread(target=self.serial_thread.run) self.serial_thread_thread.start() def initUI(self): # 创建用于显示员工信息的控件 self.info_label = QLabel("员工信息", self) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", self) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", self) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") self.six_label = QLabel("性别:", self) self.six_label.move(70, 200) self.six_label.setStyleSheet("font-size: 18px; color: black;") self.sfz_label = QLabel("身份证:", self) self.sfz_label.move(70, 250) self.sfz_label.setStyleSheet("font-size: 18px; color: black;") self.tel_label = QLabel("电话:", self) self.tel_label.move(70, 300) self.tel_label.setStyleSheet("font-size: 18px; color: black;") self.setFixedSize(800, 500) self.setWindowTitle('员工信息') self.back_button = QPushButton('返回主界面', self) self.back_button.move(600, 400) self.back_button.clicked.connect(self.back_to_main) # 查询员工信息 def query_employee(self, id): conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset') cursor = conn.cursor() cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % id) result = cursor.fetchone() conn.close() return result # 处理读取到的数据 def handle_data(self, data): # 查询员工信息 result = self.query_employee(data) if result: # 更新UI界面 self.id_label.setText("员工ID:" + result[0]) self.name_label.setText("姓名:" + str(result[1])) self.six_label.setText("性别:" + result[2]) self.sfz_label.setText("身份证:" + str(result[3])) self.tel_label.setText("电话:" + result[4]) print(result[0],result[1],result[2],result[3],result[4]) else: # 显示空白信息 self.id_label.setText("员工ID:") self.name_label.setText("姓名:") self.six_label.setText("性别:") self.sfz_label.setText("身份证:") self.tel_label.setText("电话:") def closeEvent(self, event): # 关闭串口 self.serial_thread.ser.close()怎么才能再点击返回主界面按钮时,退出串口连接
时间: 2024-02-12 20:03:33 浏览: 61
您可以在返回主界面按钮的clicked()信号的槽函数中添加self.serial_thread.ser.close()代码以关闭串口连接。具体来说,可以将back_to_main()方法修改如下:
```
def back_to_main(self):
# 关闭串口连接
self.serial_thread.ser.close()
# 返回主界面
self.close()
```
这样在点击返回主界面按钮时,会先关闭串口连接,然后关闭当前窗口,返回主界面。
相关问题
class NextWindow(QMainWindow): def __init__(self, user_id): super().__init__() self.user_id = user_id self.initUI() # 创建串口线程 self.serial_thread = SerialThread() self.serial_thread.data_received.connect(self.handle_data) self.serial_thread_thread = threading.Thread(target=self.serial_thread.run) self.serial_thread_thread.start() def initUI(self): # 创建用于显示员工信息的控件 self.info_label = QLabel("员工信息", self) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", self) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", self) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") self.six_label = QLabel("性别:", self) self.six_label.move(70, 200) self.six_label.setStyleSheet("font-size: 18px; color: black;") self.sfz_label = QLabel("身份证:", self) self.sfz_label.move(70, 250) self.sfz_label.setStyleSheet("font-size: 18px; color: black;") self.tel_label = QLabel("电话:", self) self.tel_label.move(70, 300) self.tel_label.setStyleSheet("font-size: 18px; color: black;") self.setFixedSize(800, 500) self.setWindowTitle('员工信息')中添加一个返回主界面的按钮
可以在initUI()函数中添加一个返回主界面的按钮,代码如下:
```
self.back_button = QPushButton('返回主界面', self)
self.back_button.move(600, 400)
self.back_button.clicked.connect(self.back_to_main)
```
其中,back_to_main()是一个自定义的函数,用于实现点击按钮后返回主界面的功能。你需要在NextWindow类中定义这个函数,代码如下:
```
def back_to_main(self):
self.close() # 关闭当前界面
self.parent().show() # 显示主界面
```
在这个函数中,我们先关闭当前界面,然后通过parent()方法获取到父窗口(即主界面),并将其显示出来。
class LoginWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setFixedSize(800, 500) self.setWindowTitle('鸡爪称重系统') # 创建一个QStackedWidget控件 self.stacked_widget = QStackedWidget(self) self.stacked_widget.setGeometry(0, 0, 800, 500) # 创建Login界面 login_widget = QWidget() self.stacked_widget.addWidget(login_widget) # 在Login界面上创建一个按钮 self.button = QPushButton('开始称重', login_widget) self.button.move(100, 100) self.button.setGeometry(300, 200, 200, 50)怎么实现点击按钮跳转到class NextWindow(QWidget): def __init__(self, user_id): super().__init__() self.user_id = user_id self.initUI() # 打开串口 self.ser = serial.Serial('COM7', 9600, timeout=1) def initUI(self): # 创建用于显示员工信息的控件 self.info_label = QLabel("员工信息", self) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", self) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", self) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") self.six_label = QLabel("性别:", self) self.six_label.move(70, 200) self.six_label.setStyleSheet("font-size: 18px; color: black;") self.sfz_label = QLabel("身份证:", self) self.sfz_label.move(70, 250) self.sfz_label.setStyleSheet("font-size: 18px; color: black;") self.tel_label = QLabel("电话:", self) self.tel_label.move(70, 300) self.tel_label.setStyleSheet("font-size: 18px; color: black;") self.setFixedSize(800, 500) self.setWindowTitle('员工信息')
在按钮的点击事件中添加以下代码实现跳转:
``` python
self.button.clicked.connect(self.go_to_next_window)
def go_to_next_window(self):
next_window = NextWindow(user_id) # 创建NextWindow实例
self.stacked_widget.addWidget(next_window) # 将NextWindow添加到QStackedWidget中
self.stacked_widget.setCurrentWidget(next_window) # 切换到NextWindow
```
注意,需要先导入NextWindow类。
阅读全文