def initUI(self): # 连接数据库,获取员工信息 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'" % self.user_id) result = cursor.fetchone() conn.close() # 创建用于显示员工信息的控件 info_label = QLabel("员工信息", self) info_label.move(100, 50) info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") id_label = QLabel("员工ID:" + result[0], self) id_label.move(70, 100) id_label.setStyleSheet("font-size: 18px; color: black;") name_label = QLabel("姓名:" + str(result[1]), self) name_label.move(70, 150) name_label.setStyleSheet("font-size: 18px; color: black;") six_label = QLabel("性别:" + result[2], self) six_label.move(70, 200) six_label.setStyleSheet("font-size: 18px; color: black;") sfz_label = QLabel("身份证:" + str(result[3]), self) sfz_label.move(70, 250) sfz_label.setStyleSheet("font-size: 18px; color: black;") tel_label = QLabel("电话:" + result[4], self) tel_label.move(70, 300) tel_label.setStyleSheet("font-size: 18px; color: black;")显示空白信息,根据import serial # 打开串口 ser = serial.Serial('COM1', 9600, timeout=1) # 读取数据 while True: data = ser.readline() if data: print(data.decode().strip()) # 关闭串口 ser.close()返回的员工ID,再MySQL中查询员工信息并展示
时间: 2024-01-10 22:03:16 浏览: 27
为了根据串口数据查询MySQL中的员工信息并展示在UI界面上,你需要在串口数据读取的地方,调用查询员工信息的函数,然后将查询结果展示在UI界面上。以下是修改后的示例代码:
```python
import serial
import pymysql
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MainWindow(QMainWindow):
def __init__(self, user_id):
super().__init__()
self.user_id = user_id
self.initUI()
# 打开串口
self.ser = serial.Serial('COM1', 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;")
# 查询员工信息
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])
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()
if __name__ == '__main__':
app = QApplication([])
window = MainWindow("001")
window.show()
window.read_data()
app.exec_()
```
在上面的代码中,我们在`MainWindow`类中定义了一个`query_employee()`函数来查询员工信息,该函数接受一个ID参数并返回一个元组,包含员工的姓名、性别、身份证和电话。然后,在`read_data()`函数中,我们读取串口数据并调用`query_employee()`函数查询员工信息,并将查询结果更新到UI界面上。
请注意,上面的代码仅供参考,你需要根据你的具体需求修改它以适应你的应用场景。例如,你可能需要将员工信息显示在不同的UI控件中,或者使用不同的布局方式来展示员工信息。