Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,使用openCV显示图像并使用Tkinter创建单独的窗口会导致崩溃_Python_Opencv_Tkinter - Fatal编程技术网

在Python中,使用openCV显示图像并使用Tkinter创建单独的窗口会导致崩溃

在Python中,使用openCV显示图像并使用Tkinter创建单独的窗口会导致崩溃,python,opencv,tkinter,Python,Opencv,Tkinter,我尝试使用Python脚本来完成两件事: 对于所有图像/图像标题对: 通过cv2.imshow(…) 通过tkintertk.Text(self.root) 但是,这会导致崩溃。或者至少……它通常会导致崩溃(它会输出一系列奇怪的错误,然后python[或ipython,取决于我使用的是哪种类型]退出)。这些错误的形式如下: Python[50566:37729544]***由于未捕获的异常NSInvalidArgumentException'而终止应用程序,原因:'-[NSApplicati

我尝试使用Python脚本来完成两件事:

对于所有图像/图像标题对:

  • 通过
    cv2.imshow(…)
  • 通过tkinter
    tk.Text(self.root)
但是,这会导致崩溃。或者至少……它通常会导致崩溃(它会输出一系列奇怪的错误,然后python[或ipython,取决于我使用的是哪种类型]退出)。这些错误的形式如下:

Python[50566:37729544]***由于未捕获的异常NSInvalidArgumentException'而终止应用程序,原因:'-[NSApplication\u setup:::]:未识别的选择器已发送到实例0x7fb960319cc0'
如果我不使用openCV显示图像,我可以消除这些错误。我怀疑这与
cv2.imshow
和Tk之间的交互有关,或者我的Tk代码编写得有多糟糕,但即使是这种可笑的黑客工作也花了很长时间才完成。我不确定这是否是因为我是个白痴,但Tk文档似乎非常复杂(即使是关于Tk的简单问题似乎也有以下形式的答案:“当然你能做到!但要给你一个快速的答案是完全不可能的;首先,你必须仔细阅读错综复杂的文档,了解如何使用Tk进行回调、切换、行话和我以前从未听过的单词。祝你玩得开心!”)

生成可编辑标题窗口的代码是这样的。我敢肯定,在使用Tk时,它违反了许多原则:

class editcap():
    # allows editing of the captions
    def __init__(self, caption):
        self.precap = caption
    def get(self):
        self.caption = self.text.get("1.0",tk.END)
        self.root.destroy()
    def getesc(self, event):
        self.caption = event.widget.get("1.0",tk.END)
        self.root.destroy()
    def disp(self):
        self.root = tk.Tk() # it crashes here 
        self.text = tk.Text(self.root)
        self.text.insert(tk.INSERT, self.precap)
        self.text.pack()
        submit = tk.Button(self.root, text ="Submit", command = self.get)
        submit.pack(side = tk.BOTTOM)
        self.text.bind('<Escape>', self.getesc)
        self.root.mainloop()
        return self.caption

也可以显示OpenCV代码吗?尝试在
cv2.imshow(…)
行后添加
cv2.waitKey(1)
(等待一次按键,等待1毫秒,即根本不等待)或
cv2.waitKey(0)
(永远等待按键)。我怀疑这不是问题。它通过
rawinput()暂停任何一种方式
--在哪些情况下,正确绘制图像是很困难的。只有在我提出同样的要求时,它才会尝试编辑标题,这可能发生在它被绘制到屏幕上后的数百毫秒甚至数千毫秒。但我还是会尝试!是的,唉,它没有工作:-(找到解决方案了吗?
def display_img(post):
    img = get_image(post) # saves the image from web if it hasn't already been saved, load via plt.imread()
    if resample_to != None:
        wxr = resample_to * 1. / img.shape[1]
        hx = img.shape[0] * wxr
        img = cv2.resize(cv2.cvtColor(img, cv2.COLOR_RGB2BGR), (resample_to, int(hx)))
    cv2.imshow('img',img)