class MainWindow(QMainWindow): 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('员工信息') # 查询员工信息 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 read_data(self): data = self.ser.readline() if data: # 解析数据 id = data.decode().strip() # 查询员工信息 result = self.query_employee(id) 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("电话:") # 定时读取数据 QTimer.singleShot(100, self.read_data) def closeEvent(self, event): # 关闭串口 self.ser.close()用多线程改写代码,防止主线程阻塞
时间: 2024-02-09 21:13:19 浏览: 175
最终的mainwindowh文件如下。-QT实验设计PPT
Here's an example of how you could use multithreading to prevent the main thread from blocking:
```python
from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal
class SerialThread(QThread):
dataReceived = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.ser = serial.Serial('COM7', 9600, timeout=1)
self.running = False
def run(self):
self.running = True
while self.running:
data = self.ser.readline().decode().strip()
if data:
self.dataReceived.emit(data)
def stop(self):
self.running = False
self.wait()
self.ser.close()
class MainWindow(QMainWindow):
def __init__(self, user_id):
super().__init__()
self.user_id = user_id
self.initUI()
self.serialThread = SerialThread(self)
self.serialThread.dataReceived.connect(self.handle_data)
self.serialThread.start()
def initUI(self):
# Create UI elements here
def handle_data(self, data):
# Handle incoming data here
result = self.query_employee(data)
if result:
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 query_employee(self, id):
# Query employee information from database
pass
def closeEvent(self, event):
self.serialThread.stop()
```
In this example, a `SerialThread` is created to handle the serial communication. The `dataReceived` signal is emitted whenever new data is available. The `handle_data` method is called whenever the signal is emitted, and this method updates the UI with the relevant employee information. The `SerialThread` is started in the `MainWindow` constructor, and stopped in the `closeEvent` method.
阅读全文