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 浏览: 32
要在 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 没有定义,如果没有在程序中定义,会导致程序出错。

相关推荐

import cv2 import sys import os import time from PyQt5 import QtGui #重新导入 from PyQt5 import QtCore #重新导入 from showPic import Ui_MainWindow from PyQt5.QtCore import * from PyQt5.QtGui import * #导入的外面 from PyQt5.QtWidgets import * camera_path = 0 # 0:自带摄像头 1:外接摄像头 "xxx.mp4" "rtsp://admin:pwd@192.168.2.10/cam/..." capture = cv2.VideoCapture(camera_path) # 初始化播放器 流媒体 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # XVID/DIVX MPEG MJPG X264 video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸) class videoShow(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.dir_path = r"E:\pycharm\new_subject\image/" self.pushButton_play.clicked.connect(self.play_video) self.pushButton_pause.clicked.connect(self.pause_video) def play_video(self): self.playing = True def pause_video(self): self.playing = False def timer_pic(self): image_name = self.dir_path + self.file_list[self.n] url = image_name pic_image = cv2.imread(url) pic_image = cv2.cvtColor(pic_image, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换成RGB height, width = pic_image.shape[:2] pixMap = QImage(pic_image.data, width, height, width*3, QImage.Format_RGB888) # 将RGB格式图像转换为八位图 pixMap = QPixmap.fromImage(pixMap) ratio = max(width/self.label.width(), height/self.label.height()) pixMap.setDevicePixelRatio(ratio) # 根据图片比例显示 self.label.setAlignment(Qt.AlignCenter) # 设置居中 self.label.setPixmap(pixMap) if self.playing: flag, frame = capture.read() if flag is False: return frame = cv2.resize(frame, (960, 540)) video_writer.write(frame) cv2.namedWindow("video", 0) cv2.imshow("video", frame) key = cv2.waitKey(25) if key == 27: video_writer.release() cv2.destroyAllWindows() sys.exit(0) if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_()) 优化这段代码,实现录制视频以及点击按钮实现录制、播放、暂停

最新推荐

recommend-type

PyQt5中多线程模块QThread使用方法的实现

主要介绍了PyQt5中多线程模块QThread使用方法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

python GUI库图形界面开发之PyQt5线程类QThread详细使用方法

def __init __(self): super(Thread,self).__ init __() def run(self): #线程相关的代码 pass 接下来创建一个新的线程 thread = Thread() thread.start() 可以看出,PyQt的线程使用非常简单—...
recommend-type

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip
recommend-type

java 游戏飞翔的小鸟

java 制作游戏 飞翔的小鸟
recommend-type

setuptools-25.3.0.zip

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
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

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

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