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()
时间: 2024-01-01 08:06:32 浏览: 165
PYQT5、PYQT5-Tools离线安装包
要在 PyQt5 中显示这段代码,可以使用 QTextEdit 或者 QPlainTextEdit 组件。首先,需要在程序中导入相关的模块:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit
```
然后,创建一个 QTextEdit 组件,并将代码添加到其中:
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.code_edit = QTextEdit(self)
self.code_edit.setReadOnly(True)
self.setCentralWidget(self.code_edit)
self.code_edit.setPlainText('''
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(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()
''')
```
这样就可以将代码显示在程序的主窗口中了。需要注意的是,这段代码中的 self.ser 没有定义,如果没有在程序中定义,会导致程序出错。
阅读全文