pythonclassuser-interfacepyqt5qcheckbox

Can I use a variable from a function that's inside of another class?


What I'm trying to do is create 1 window with 8 check boxes. The user will click the ones that are relevant and press "Calculate Job". That button should then open a new window that contains buttons/sections/whatever for only the options that were selected in window 1. Sounds ok... I have window 1 set up in class First. When I click the button it shows up the new window, great! But now I need to find a way to say "If any of these sections are selected then do something in window 2".

code:

from PyQt5 import QtWidgets
import sys
import qdarkstyle

class First(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.title = 'Job Price Calculator'
        self.left = 100
        self.top = 100
        self.width = 320
        self.height = 350
        self.initUI()
        self.dialog = Second()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
        self.data_processing.move(50, 50)

        self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
        self.digital_print.move(50, 100)

        self.calculate = QtWidgets.QPushButton('Calculate Job', self)
        self.calculate.move(100, 250)
        self.calculate.clicked.connect(self.on_button_click)

    def on_button_click(self):
        self.dialog.show()

class Second(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        self.title = 'Job Specification'
        self.left = 500
        self.top = 100
        self.width = 1080
        self.height = 920
        self.initUI_specification()

    def initUI_specification(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())

        if self.data_processing.isChecked():
            self.data_processing_info()

        if self.digital_print.isChecked():
            self.digital_print_info()

    def data_processing_info(self):
        self.temp_button = QtWidgets.QPushButton('Temp Button', self)
        self.temp_button.move(100, 250)

    def digital_print_info(self):
        self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
        self.temp_button2.move(100, 450)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())

So what I want to do in class 'Second' is use the checkbox variable of self.data_processing to check if it has been enabled, if it has then I want to pull up a button in the Second gui window.

Is what I'm trying to do possible? Should I be thinking of a different way to do this? I'd like someones opinion on this if possible, and a little bit of guidance. I'd really appreciate the help, I've spent my whole weekend messing around with this and I'm just getting nowhere.


Solution

  • You need to call your second class inside first class when your checkbox is clicked

    from PyQt5 import QtWidgets
    import sys
    import qdarkstyle
    
    class First(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(First, self).__init__(parent)
            self.title = 'Job Price Calculator'
            self.left = 100
            self.top = 100
            self.width = 320
            self.height = 350
            self.initUI()
            
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
            self.data_processing.move(50, 50)
            self.data_processing.stateChanged.connect(self.checkbox_clicked)
    
            self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
            self.digital_print.move(50, 100)
    
            self.calculate = QtWidgets.QPushButton('Calculate Job', self)
            self.calculate.move(100, 250)
            self.calculate.clicked.connect(self.on_button_click)
    
            self.secondClass = Second()
    
        def on_button_click(self):
            self.dialog.show()
        
        def checkbox_clicked(self):
            if self.data_processing.isChecked():
                self.secondClass.data_processing_info()
                self.secondClass.show()
            else:
                self.secondClass.hide() #hide or close - as your requirement
    
    class Second(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(Second, self).__init__(parent)
            self.title = 'Job Specification'
            self.left = 500
            self.top = 100
            self.width = 1080
            self.height = 920
            self.initUI_specification()
    
        def initUI_specification(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
            self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    
    
        def data_processing_info(self):
            self.temp_button = QtWidgets.QPushButton('Temp Button', self)
            self.temp_button.move(100, 250)
    
        def digital_print_info(self):
            self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
            self.temp_button2.move(100, 450)
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        main = First()
        main.show()
        sys.exit(app.exec_())