如何在 Python3 和 PyQt5 中正确地将整数变量从一个函数传递到另一个函数?

How to correctly pass an integer variable from one function to another in Python3 and PyQt5?

编辑 我已经将下面的代码更新为我认为正确的代码,但是我仍然没有得到所需的输出,我还包括了如何在我的 mainWindow class...

中调用这两个函数

我在如下所示的 GUI 程序中的两个函数之间传递整数时遇到问题。我正在使用 Python3.7 和 pyqt5.

     def capture_duration(self, item):#recieves file list item from file_list
        fn = item.text() #extract text data of file, ie file name
        url = qtc.QUrl.fromLocalFile(self.video_dir.filePath(fn)) #get URL of file
        print(url) #unforunately PyQt5 doesn't give a clean file path to hand to openCV
        url = url.toString() #convert 'URL' to string
        url.strip("PyQt5.QtCore.QUrl('/") #strip string of everything before the C.
        print(url) #print for testing only
        data = cv2.VideoCapture(url) #start video capture with openCV
        frames = data.get(cv2.CAP_PROP_FRAME_COUNT) #get number of frames of video
        fps = int(data.get(cv2.CAP_PROP_FPS)) #get fps of video
        duration = int(frames/fps) #compute frames/fps for total duration in seconds
        print("Duration of video is:", duration) #print for test purposes only.
        return duration

    def imgacq(self, duration):#FPS of camera averages 54FPS
        print('Duration is', duration)
        num_frames = (duration*54) #duration of video in seconds multiplied by recording frame rate of camera.
        print(num_frames)
        with Camera() as cam:
            if 'Bayer' in cam.PixelFormat:
                cam.PixelFormat = 'RGB8'

        cam.OffsetX = 0
        cam.OffsetY = 0
        cam.Width = cam.SensorWidth
        cam.Height = cam.SensorHeight

        self.statusBar().showMessage('Opened camera %s (#%s), now recording...' % (cam.DeviceModelName, cam.DeviceSerialNumber))
        cam.start()
        start = time.time()

        imgs = [cam.get_array() for n in range(num_frames)] #num frames must = number of frames in selected video.

        el = time.time() - start
        cam.stop()

        print('Acquired %d images in %.2f s (~ %.1f fps)' % (len(imgs), el, len(imgs) / el))

        # Make a directory to save some images
        output_dir = 'test_images'
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        print('Saving to "%s"' % output_dir)

        # Save them
        for n, img in enumerate(imgs):
            Image.fromarray(img).save(os.path.join(output_dir, '%08d.jpg' % n))

这两个函数在我的主窗口 class 中调用如下,其中 capture_duration 在文件查看器中单击视频文件时启动,而 imgacq 在工具栏被点击:

self.file_list.itemClicked.connect(self.capture_duration)
play_action.triggered.connect(self.imgacq)

当我 运行 这个 'duration' 在 'capture_duration' 中被正确打印为一个整数但是当传递给 'imgacq' 它被打印为 False 而不是具体的整数值。我对这段代码的逻辑是 capture_duration 从 mainWindow class 继承了一些信息,进行计算并 returns durationimgacq 然后继承 duration 并相应地使用它,但它显然不是那样工作的。

我确定这是我犯的一个非常基本的错误,但我被卡住了,非常感谢任何帮助!

imagecq 打印 False 的原因是信号 play_action.triggered 发出动作的 checked 状态(即 False)。然后将其分配给 imagecq 的输入参数:duration.

另一个问题是Qt widgets 会忽略slot 产生的任何输出,所以当slot 被触发时capture_duration 的返回值将会丢失。解决此问题的一种选择是将 duration 的值分配给非局部变量,例如实例变量,而不是(或除此之外)返回它。所以在你的情况下你可以做这样的事情:

def capture_duration(self, item):#recieves file list item from file_list
    ....
    self.duration = duration
    return duration   # this return value will be ignored if capture_duration is used as a slot so could be omitted

然后在imagcq:

# Note: input parameter has been omitted. instance variable is used instead
def imgacq(self):   
    duration = self.duration
    .....

此外,您需要在显示主要 window 之前将 self.duration 初始化为某个合理的值,以防止在 capture_duration 之前调用 imacq 时出现错误。