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-11 14:07:03 浏览: 148
QT_UART.zip
好的,我可以帮你把这个程序改成多线程的形式。首先,你需要导入 `QThread` 模块。
然后,在 `MainWindow` 类中,我们创建一个新的类 `SerialThread`,继承自 `QThread`。
在 `SerialThread` 类中,我们重写 `run` 方法,将其中的串口读取数据的代码放入其中。
在 `MainWindow` 类中,我们创建一个 `SerialThread` 对象,并在 `init` 方法中启动它。
下面是修改后的代码:
```python
from PyQt5.QtCore import QThread, QTimer
class SerialThread(QThread):
def __init__(self, ser, query_employee_callback):
super().__init__()
self.ser = ser
self.query_employee_callback = query_employee_callback
def run(self):
while True:
data = self.ser.readline()
if data:
id = data.decode().strip()
result = self.query_employee_callback(id)
self.emit_data(result)
def emit_data(self, result):
self.emit(SIGNAL("updateData(PyQt_PyObject)"), result)
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)
# 创建并启动串口读取线程
self.serial_thread = SerialThread(self.ser, self.query_employee)
self.serial_thread.start()
self.connect(self.serial_thread, SIGNAL("updateData(PyQt_PyObject)"), self.update_data)
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
# 更新UI界面
def update_data(self, result):
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 closeEvent(self, event):
# 关闭串口
self.ser.close()
```
请注意,由于串口读取是一个死循环,因此这个程序将一直运行,直到你关闭窗口。
阅读全文