0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
mfrwll
  • 3
  • 2
  • `self.dialog = Second` - `def on_button_click(self): self.dialog(param 1, param2, param3, param4).show()` - then do something with param_n_ in `class Second(..): def __init__(self, param1, param2, param3, param4, parent=None):` ? – Patrick Artner Dec 21 '20 at 09:10
  • or do a `def on_button_click(self): self.dialog.setup(param1,param2, ...); self.dialog.show()` to setup your 2nd dialog with options. – Patrick Artner Dec 21 '20 at 09:12

1 Answers1

0

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_())
astrick
  • 190
  • 1
  • 9
  • I see.. However when I click the "data processing" box it now opens a new window instantly.. What I need to do is to click all of the ones I want and then when I click the calculate button it will open the new window with those sections inside... Does that make sense? – mfrwll Dec 21 '20 at 10:52
  • Yes you can do that. – astrick Dec 21 '20 at 11:47
  • I don't understand how I can use this for ticking multiple check boxes though. Let's say a user ticked 6 out of 8 check boxes, how can I show 6 different functions in that second window? The below code doesn't work if I tick both boxes: def checkbox_clicked(self): if self.data_processing.isChecked(): self.secondClass = Second() self.secondClass.data_processing_info() elif self.digital_print.isChecked(): self.secondClass = Second() self.secondClass.digital_print_info() – mfrwll Dec 21 '20 at 12:34
  • you can change that as per your need. I would suggest you to create only one function and pass object name as a parameter. So whenever any checkbox is clicked that same function is having object of clicked checkbox and can be used to update your variable – astrick Dec 21 '20 at 12:42