def start(): global img, pre_pts while True: root = tk.Tk() root.withdraw() f_path = filedialog.askopenfilename() if f_path: img = cv.imread(f_path) break else: messagebox.showinfo("提示", "请选择文件!") 修改上述代码,使得打开本地文件夹不选文件或者关闭系统窗口后,使得下面的主界面窗口按钮仍能够触发start()函数class Faceshow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(Faceshow, self).__init__(parent) self.setupUi(self) self.main_button.clicked.connect(self.show_resu) self.main_exit.clicked.connect(self.quit) self.form = None def show_resu(self): img1, img2, img3, img4, text1, text2 = start() img1 = ndarray_to_qpixmap(img1) img2 = ndarray_to_qpixmap(img2) img3 = ndarray_to_qpixmap(img3) img4 = ndarray_to_qpixmap(img4) self.form = RESUshow(img1, img2, img3, img4, text1, text2) self.form.pushButton.clicked.connect(self.form.save_img4) self.form.show()
时间: 2023-06-26 16:05:32 浏览: 101
修改后的代码如下:
```python
def start():
global img, pre_pts
f_path = filedialog.askopenfilename()
if f_path:
img = cv.imread(f_path)
return img
else:
return None
class Faceshow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Faceshow, self).__init__(parent)
self.setupUi(self)
self.main_button.clicked.connect(self.show_resu)
self.main_exit.clicked.connect(self.quit)
self.form = None
def show_resu(self):
img1 = start()
if img1 is None:
return
img2, img3, img4, text1, text2 = process_image(img1)
img1 = ndarray_to_qpixmap(img1)
img2 = ndarray_to_qpixmap(img2)
img3 = ndarray_to_qpixmap(img3)
img4 = ndarray_to_qpixmap(img4)
self.form = RESUshow(img1, img2, img3, img4, text1, text2)
self.form.pushButton.clicked.connect(self.form.save_img4)
self.form.show()
```
在修改后的代码中,将 `start()` 函数中的循环改为了单次调用,如果用户没有选择文件,则直接返回 `None` 值,不会阻塞主界面窗口按钮的触发。在 `show_resu()` 函数中,首先调用 `start()` 函数获取图像,如果返回值为 `None` 则直接返回,不再进行后续的图像处理和显示。
阅读全文