我需要重新激活一些旧代码,其中包括 while(cv.getWindowProperty(msg, 1)) >= 0):以判断窗口何时关闭.我已经将我的Python降级到3.8,并安装了所有从那时起的版本的包,但我仍然收到一些错误

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window_w32.cpp:837: error: (-27:Null pointer) NULL window: 'Window name' in function 'cvGetPropWindowAutoSize_W32'

我不知道为什么,因为我不能调试到getWindowProperty(). 有没有人已经遇到过这个错误?

推荐答案

这有一个特定的属性:WND_PROP_VISIBLE.这一制度自2016年底以来一直存在,并于https://github.com/opencv/opencv/pull/7561年引入

if cv.getWindowProperty("foo", cv.WND_PROP_VISIBLE):
    print("it's there")
else:
    print("it's not there")

或者,查询任何其他属性,然后捕获异常并对其作出react .

import cv2 as cv # recommended way to import OpenCV

def does_the_window_exist(winname):
    try:
        cv.getWindowProperty(winname, cv.WND_PROP_AUTOSIZE) # any property
        return True # that worked, so it exists
    except cv.error as e:
        if e.code == -27: # (-27:Null pointer) lookup didn't find such a window
            return False
        else:
            raise # some unanticipated error

print(does_the_window_exist("foo")) # => False

Python相关问答推荐

带有pandas的分区列上的过滤器的多个条件read_parquet

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

KNN分类器中的GridSearchCV

将numpy数组存储在原始二进制文件中

根据不同列的值在收件箱中移动数据

需要计算60,000个坐标之间的距离

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

OR—Tools中CP—SAT求解器的IntVar设置值

转换为浮点,pandas字符串列,混合千和十进制分隔符

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

在两极中过滤

在Python中使用if else或使用regex将二进制数据如111转换为001""

在输入行运行时停止代码

将标签移动到matplotlib饼图中楔形块的开始处

如何创建引用列表并分配值的Systemrame列

如何找出Pandas 图中的连续空值(NaN)?

判断Python操作:如何从字面上得到所有decorator ?

如何根据rame中的列值分别分组值

统计numpy. ndarray中的项目列表出现次数的最快方法

如何强制向量中的特定元素在Gekko中处于优化解决方案中