python - 如何在 Python3 和 PyQt5 中正确地将整数变量从一个函数传递到另一个函数?
问题描述
编辑 我已经将下面的代码更新为我认为正确的代码,但是我仍然没有得到所需的输出,我还包括了如何在我的 mainWindow 类中调用这两个函数......
我在我的 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))
这两个函数在我的 mainWindow 类中调用如下,在capture_duration
文件查看器中单击视频文件时imgacq
开始,单击工具栏上的播放按钮时开始:
self.file_list.itemClicked.connect(self.capture_duration)
play_action.triggered.connect(self.imgacq)
当我运行时,这个“持续时间”被正确打印为“capture_duration”中的整数,但是当传递给“imgacq”时,它被打印为False
而不是特定的整数值。我对这段代码的逻辑是capture_duration
从 mainWindow 类继承一些信息,进行计算并返回duration
。imgacq
然后继承duration
并相应地使用它,但它显然不是那样工作的。
我确定这是我正在犯的一个非常基本的错误,但我被卡住了,非常感谢任何帮助!
解决方案
打印 False的原因imagecq
是因为信号play_action.triggered
发出了checked
动作的状态(即 False)。然后将其分配给imagecq
:的输入参数duration
。
另一个问题是 Qt 小部件会忽略插槽生成的任何输出,因此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
.....
此外,您需要self.duration
在显示主窗口之前初始化一些合理的值,以防止在imacq
调用之前发生错误capture_duration
。
推荐阅读
- list - 列表:将推文从一个列表过滤到另一个列表 - Twitter API
- python - 我可以将关键字作为值存储到变量中吗?
- python - 修补 Django FileSystemStorage 位置不生效 [pytest]
- ruby-on-rails - Rais 应用程序用户特定索引向所有用户显示
- reporting-services - SSRS 条件可见性问题
- python - google api v6在python中将枚举显示为值而不是名称
- javascript - 如何从具有类似类的 HTML 中获取某些元素(使用 JavaScript)?
- java - 为什么 Selenium 无法识别 WebElement?
- javascript - 错误:当您双击(切换)选择时,Select2 Ajax 多选复选框不会删除(取消选择)
- python - 创建(正常)均值和标准差的 2 层级估计