def start(): global img, pre_pts 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)上述代码报错时,下列主界面会卡住并退出程序:class Faceshow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(Faceshow, self).__init__(parent) self.setupUi(self) self.main_button.clicked.connect(start) self.main_exit.clicked.connect(self.quit) def quit(self): self.close() if __name__ == '__main__': hxz = QApplication(sys.argv) ui = Faceshow() ui.show() sys.exit((hxz.exec_()))
时间: 2023-06-13 08:08:13 浏览: 99
新加坡南洋理工的Matlab的OFDM经典教材-南洋理工-Chapter_6_OFDM_Notes.pdf
可能是因为出现了异常,但是异常没有被捕获,导致程序直接退出。你可以在 `start()` 函数中加上异常处理,例如用 `try...except` 块包裹代码,打印出错误信息,然后使程序继续运行。这样就可以避免程序直接退出。修改后的代码如下:
```
def start():
global img, pre_pts
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)
traceback.print_exc()
class Faceshow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Faceshow, self).__init__(parent)
self.setupUi(self)
self.main_button.clicked.connect(start)
self.main_exit.clicked.connect(self.quit)
def quit(self):
self.close()
if __name__ == '__main__':
hxz = QApplication(sys.argv)
ui = Faceshow()
ui.show()
sys.exit((hxz.exec_()))
```
这样程序就不会卡住并退出了,而是会在控制台中打印出错误信息,方便你进行调试。
阅读全文