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控件中,或者使用不同的布局方式来展示员工信息。

相关推荐

class EmployeeInfoWindow(QWidget): def init(self, user_id): super().init() self.user_id = user_id self.initUI() 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, 100) name_label = QLabel("姓名:" + result[1], self) name_label.move(100, 150) age_label = QLabel("年龄:" + str(result[2]), self) age_label.move(100, 200) # TODO: 创建其他员工信息控件 # 设置窗口大小和标题 self.setFixedSize(800, 500) self.setWindowTitle('员工信息') class LoginWindow(QWidget): def init(self): super().init() self.initUI() def initUI(self): self.setFixedSize(800, 500) self.setWindowTitle('鸡爪称重系统') # 创建一个按钮 button = QPushButton('开始称重', self) button.move(100, 100) button.setGeometry(300, 200, 200, 50) button.clicked.connect(self.showInputDialog) def showInputDialog(self): # 创建一个输入框,获取员工ID text, okPressed = QInputDialog.getText(self, "输入员工ID", "输入员工ID:") if okPressed and text != '': # 连接数据库,检查员工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'" % text) result = cursor.fetchone() conn.close() if result: # 员工ID存在,跳转到下一个界面,显示员工信息 print('输入的ID是:', text) employee_info_window = EmployeeInfoWindow(text) employee_info_window.show() self.hide() else: # 员工ID不存在,弹出提示框,提示重新输入 QMessageBox.warning(self, '错误', '员工ID不存在,请重新输入!') if name == 'main': app = QApplication(sys.argv) loginWindow = LoginWindow() loginWindow.show() sys.exit(app.exec_())怎么更改为跳转界面

class EmployeeInfoWindow(QWidget): def init(self, user_id, stacked_widget): super().init() self.user_id = user_id self.stacked_widget = stacked_widget self.initUI() 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;") # 创建返回按钮 back_button = QPushButton('返回', self) back_button.setGeometry(600, 400, 100, 50) back_button.clicked.connect(self.backToLoginWindow) # 设置窗口大小和标题 self.setFixedSize(800, 500) self.setWindowTitle('员工信息') def backToLoginWindow(self): self.stacked_widget.setCurrentIndex(0)在员工信息界面右边设置数字显示框,接收显示def get_data(): ser = serial.Serial('COM3', 9600) data = ser.readline() if data: # 将字符串按空格分割,并取第三个元素作为数值 value_str = data.decode().strip().split()[1] value = float(value_str) ser.close() return value

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()把这个改为多线程

pyqt5怎么显示class QueryThread(QThread): query_result = pyqtSignal(object) def __init__(self, id): super().__init__() self.id = id def run(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.id) result = cursor.fetchone() conn.close() self.query_result.emit(result) # 发送查询结果信号 class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.query_thread = None # 查询线程 def read_data(self): data = self.ser.readline() if data: id = data.decode().strip() if not self.query_thread: # 如果查询线程不存在,则创建并启动 self.query_thread = QueryThread(id) self.query_thread.query_result.connect(self.update_ui) self.query_thread.start() else: self.query_thread.id = id # 如果查询线程已存在,则更新查询ID QTimer.singleShot(100, self.read_data) def update_ui(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]) 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() if self.query_thread: self.query_thread.quit() self.query_thread.wait()

最新推荐

recommend-type

node-v6.3.1-linux-ppc64.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

计算机专业词汇+英语+计算机能不学英语吗?

计算机专业英语词汇非常丰富,涉及计算机硬件、软件、网络、程序设计语言等多个方面。由于篇幅限制,我无法直接列出完整的1000个计算机专业英语词汇,但我可以为您提供一些常见的计算机专业英语词汇作为示例: file - 文件 command - 命令,指令 use - 使用,用途 program - 程序 line - (数据,程序)行,线路 if - 如果(连词) display - 显示,显示器 set - 设置(动词),集合(名词) key - 键,关键字,关键码 list - 列表,显示(名词),打印(动词) by - 凭,靠,沿(介词) press - 按,压(动词) with - 用,与,随着(介词) format - 格式 change - 更换,改变,变动(动词)
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

输出这段Python代码输出所有3位整数中,个位是5且是3的倍数的整数

``` for i in range(100,1000): if i%10 == 5 and i%3 == 0: print(i) ``` 输出结果: ``` 105 135 165 195 225 255 285 315 345 375 405 435 465 495 525 555 585 615 645 675 705 735 765 795 825 855 885 915 945 975 ```