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) 报错后如何修改可以使下面主界面可以重新点击执行start函数,且改start函数和主界面不在同一个py文件中:class Faceshow(QMainWindow, Ui_MainWindow): def init(self, parent=None): super(Faceshow, self).init(parent) self.setupUi(self) self.main_button.clicked.connect(start)
时间: 2023-06-13 08:08:34 浏览: 111
你可以在 start 函数结尾处添加一个 return 语句,并在主界面中调用该函数时判断返回值是否为 None,如果是,则不进行任何操作,否则执行其他代码。具体修改如下:
```
def start():
global img, pre_pts
try:
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
if not f_path:
messagebox.showinfo('提示', '请选取有效图片')
return None
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(lambda: self.start())
def start(self):
start()
# 如果 start 函数返回值为 None,则不进行任何操作
if start() is None:
pass
else:
# 执行其他代码
pass
```
阅读全文