try: root = tk.Tk() root.withdraw() f_path = filedialog.askopenfilename() if not f_path: messagebox.showinfo('提示', '请选取有效图片') img = cv.imread(f_path) pre_pts = -1, -1 cv.namedWindow('picture', cv.WINDOW_NORMAL) cv.resizeWindow('picture', 256, 256) cv.moveWindow('picture', 600, 300) cv.imshow('picture', img) cv.setMouseCallback('picture', draw) cv.waitKey(0) cv.destroyAllWindows() except Exception as e: print("出现错误:", e) 上述代码怎么改才会使得选取本地文件界面关掉后picture窗口不再出现
时间: 2023-06-13 13:08:51 浏览: 102
你可以在选取本地文件后,加上`root.destroy()`语句,使得Tkinter窗口关闭,从而不会再打开picture窗口。修改后的代码如下:
```python
try:
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
if not f_path:
messagebox.showinfo('提示', '请选取有效图片')
else:
img = cv.imread(f_path)
pre_pts = -1, -1
cv.namedWindow('picture', cv.WINDOW_NORMAL)
cv.resizeWindow('picture', 256, 256)
cv.moveWindow('picture', 600, 300)
cv.imshow('picture', img)
cv.setMouseCallback('picture', draw)
cv.waitKey(0)
cv.destroyAllWindows()
root.destroy()
except Exception as e:
print("出现错误:", e)
```
阅读全文