PySide2 MainWindow类内QTimer/按钮无法调用函数问题求助
PySide2 MainWindow中定时器信号调用类内函数失效问题
我在使用PySide2的QMainWindow类时,遇到timer.timeout.connect()无法正常调用类内displayFrame函数的问题(按钮clicked.connect调用sys.exit正常,但调用自定义类函数存在异常)。此前未封装到MainWindow类中的代码可以正常运行,我的目标是通过TFLite和OpenCV将PiCamera的视频流推送到应用界面(该功能在非类封装版本中曾正常工作)。
故障代码
from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QMainWindow import cv2 import qimage2ndarray import sys import time import os import argparse from tflite_support.task import core from tflite_support.task import processor from tflite_support.task import vision from picamera2 import Picamera2, MappedArray, Preview import utils from imutils.video import VideoStream import imagezmq import socket from libcamera import controls
class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("My App") self.setFixedSize(QSize(480, 320)) timer = QTimer() timer.timeout.connect(self.displayFrame) timer.start(30) button = QPushButton("Quiter") label = QLabel('No Camera Feed') button.clicked.connect(sys.exit) # 此调用正常工作 layout = QVBoxLayout() layout.addWidget(button) layout.addWidget(label) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) def testing(self): print("hello") def displayFrame(self): print("hello") # 连这句都不打印 frame = picam2.capture_array("main") frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) input_tensor = vision.TensorImage.create_from_array(frame) detection_result = detector.detect(input_tensor) image = utils.visualize(frame, detection_result) sender.send_image(rpiName, image) image = qimage2ndarray.array2qimage(frame) label.setPixmap(QPixmap.fromImage(image)) app = QApplication([]) window = MainWindow() window.show() app.exec_()
此前正常运行的非类封装代码
def displayFrame(): frame = picam2.capture_array("main") frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) input_tensor = vision.TensorImage.create_from_array(frame) detection_result = detector.detect(input_tensor) image = utils.visualize(frame, detection_result) sender.send_image(rpiName, image) image = qimage2ndarray.array2qimage(frame) label.setPixmap(QPixmap.fromImage(image)) app = QApplication([]) window = QWidget() timer = QTimer() timer.timeout.connect(displayFrame) timer.start(30) label = QLabel('No Camera Feed') button = QPushButton("Quiter") button.clicked.connect(sys.exit) layout = QVBoxLayout() layout.addWidget(button) layout.addWidget(label) window.setLayout(layout) window.show() app.exec_()
测试可用的代码(仅调用简单测试函数)
class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("My App") self.setFixedSize(QSize(480, 320)) self.timer = QTimer() self.timer.timeout.connect(self.testing) # 此处本应使用self.displayFrame self.timer.start(30) button = QPushButton("Quiter") label = QLabel('No Camera Feed') button.clicked.connect(sys.exit) # 退出按钮正常工作 layout = QVBoxLayout() layout.addWidget(button) layout.addWidget(label) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) def testing(self): print("hello")
问题现象
- 上述测试代码中,终端会每30毫秒打印一次"hello",说明定时器和信号连接逻辑正常。
- 但将
self.testing改为self.displayFrame后,应用启动后立即无响应,按钮和标签均不显示,界面处于卡死状态。
内容的提问来源于stack exchange,提问作者FireFalcon3469
相关产品推荐
相关产品推荐

