pyqt5对话框self.close()不要停止

发布于 2025-02-04 19:00:04 字数 1938 浏览 0 评论 0原文

我是Python和Pyqt的新手。 我有一个带有MainWindow的应用程序和一个对话框(设置窗口)。在对话框类(加载UIC.loadui)中,我有一个段循环来进行一些OPENCV图像检测。当我调用对话框关闭时,请循环仍在运行。我该如何停止它? 谢谢 这是代码提取物,全部都在一个文件中:

class UI(QMainWindow):

    def __init__(self):
        super(UI, self).__init__()
        
        #xxxxxx MORE CODE xxxxx
        uic.loadUi("BTS.ui", self)
        self.action_Open_Settings.triggered.connect(lambda: self.open_settings_window('BAS'))
        self.show()
        
    def open_settings_window(self, target):
    # Open Settings Window
    s_window = SettingsWindow(target)
    s_window.exec()
    

class SettingsWindow(QDialog):

    def __init__(self, target):
        super(SettingsWindow,self).__init__()
        uic.loadUi('set.ui', self)
        # Settings Windows named SettingsWindow
        self.setWindowTitle(target)

        # Load Settings
        if target == 'BAS':
            self.H_MIN = B_H_MIN
            self.H_MAX = B_H_MAX
            self.S_MIN = B_S_MIN
            self.S_MAX = B_S_MAX
            self.Area_Value_Text.setEnabled(False)
            self.Peri_Value_Text.setEnabled(False)

        else:  
            self.H_MIN = R_H_MIN
            self.H_MAX = R_H_MAX
            

        self.show()
        self.run(target)

    def run(self, target):
        global gtarget
        global CROP_SIZE

        ref_image = "./set_ref.jpg"
        img = cv2.imread(ref_image)

        while True:
            print('running')

            frame = cv2.resize(img, (1280, 720))

            h_min = self.horizontalSlider_Hue_Min.value()
            h_max = self.horizontalSlider_Hue_Max.value()

            #********MORE CODE*************

            if cv2.waitKey(1) & 0xFF == ord('q'):
                SettingsWindow.close
                break


    def closeEvent(self, event):
        self.close()
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    UIWindow = UI()
    app.exec_()

I am new to python and pyqt.
I have a app with a mainwindow and a dialog (settings window). In the dialog class (loaded with uic.loadUi) I have a while loop to do some opencv image detection. When i call the dialog close the while loop still running. How can I stop it?
Thank you
This is a code extract, and is all in one file:

class UI(QMainWindow):

    def __init__(self):
        super(UI, self).__init__()
        
        #xxxxxx MORE CODE xxxxx
        uic.loadUi("BTS.ui", self)
        self.action_Open_Settings.triggered.connect(lambda: self.open_settings_window('BAS'))
        self.show()
        
    def open_settings_window(self, target):
    # Open Settings Window
    s_window = SettingsWindow(target)
    s_window.exec()
    

class SettingsWindow(QDialog):

    def __init__(self, target):
        super(SettingsWindow,self).__init__()
        uic.loadUi('set.ui', self)
        # Settings Windows named SettingsWindow
        self.setWindowTitle(target)

        # Load Settings
        if target == 'BAS':
            self.H_MIN = B_H_MIN
            self.H_MAX = B_H_MAX
            self.S_MIN = B_S_MIN
            self.S_MAX = B_S_MAX
            self.Area_Value_Text.setEnabled(False)
            self.Peri_Value_Text.setEnabled(False)

        else:  
            self.H_MIN = R_H_MIN
            self.H_MAX = R_H_MAX
            

        self.show()
        self.run(target)

    def run(self, target):
        global gtarget
        global CROP_SIZE

        ref_image = "./set_ref.jpg"
        img = cv2.imread(ref_image)

        while True:
            print('running')

            frame = cv2.resize(img, (1280, 720))

            h_min = self.horizontalSlider_Hue_Min.value()
            h_max = self.horizontalSlider_Hue_Max.value()

            #********MORE CODE*************

            if cv2.waitKey(1) & 0xFF == ord('q'):
                SettingsWindow.close
                break


    def closeEvent(self, event):
        self.close()
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    UIWindow = UI()
    app.exec_()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文