Search code examples
pythonpyqtpyqt4qcheckbox

When one checkbox is checked, by pressing a button to print some texts from a LineEdit if another checkbox is also checked


import sys, os
import PyQt4
from PyQt4 import QtGui, QtCore 
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Tab1Widget1(QWidget):
    def __init__(self, parent=None):
        super().__init__()

        self.Tab1Widget1initUI()

        self.bridge = Tab1Widget2()

    def Tab1Widget1initUI(self):
        self.setLayout(QGridLayout())

        self.T1W1_checkbox = QCheckBox('checkbox1', self)
        self.layout().addWidget(self.T1W1_checkbox, 1, 0)

    def test(self):
        print ('123')


    def run(self):
        if self.T1W1_checkbox.isChecked() == True:
            self.test()
            if self.bridge.T1W2_checkbox.isChecked() == True:
                print (self.bridge.T1W2_le.text())

class Tab1Widget2(QWidget):

    def __init__(self, parent=None):
        super().__init__()
        self.setLayout(QGridLayout())

        self.T1W2_checkbox = QCheckBox('checkbox2', self)
        self.layout().addWidget(self.T1W2_checkbox, 0, 0)

        self.T1W2_le = QLineEdit()
        self.layout().addWidget(self.T1W2_le, 0, 1)

class Tab1Layout(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setLayout(QGridLayout())

        self.group1 = Tab1Widget1(self)
        scroll = QScrollArea(self)
        scroll.setWidget(self.group1)
        scroll.setWidgetResizable(True)
        self.layout().addWidget(scroll, 0, 0)

        self.group2 = Tab1Widget2(self)
        self.layout().addWidget(self.group2, 1, 0)

        self.btnRun = QPushButton('Run', self)
        self.layout().addWidget(self.btnRun, 3, 0)
        self.btnRun.clicked.connect(self.group1.run)


class Page1(QTabWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.tab1 = Tab1Layout()
        self.addTab(self.tab1, "Tab1")

        self.tab2 = QWidget()
        self.tab3 = QWidget()
        self.addTab(self.tab2, "Tab2")
        self.addTab(self.tab3, "Tab3")
        self.tab2_initUI()
        self.tab3_initUI()

    def tab2_initUI(self):
        grid = QGridLayout()
        self.tab2.setLayout(grid)

    def tab3_initUI(self):
        grid = QGridLayout()
        self.tab3.setLayout(grid)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setGeometry(450, 250, 800, 550)
        self.startPage1()

    def startPage1(self):
        x = Page1(self)
        self.setWindowTitle("Auto Benchmark")
        self.setCentralWidget(x)
        self.show()

def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

If checkbox1 is checked and I press the run button, it will print 123. However, by pressing run button, I want checkbox2 to also print some texts entered in lineedit if the checkbox1 are also checked (i.e. it should print 123 first and then print 456).

I've looked up some similar types of questions, but none of that provides a proper answer. If anyone knows how to solve it, pls let me know thanks!!


Solution

  • The problem is that you are creating several Tab1Widget2, the first one you created in Tab1Layout, and that is the one you see, then you have created another one in Tab1Widget1, but not the time because you have not passed a parent, if you pass it to ** self ** as parent you will observe the following:

    self.bridge = Tab1Widget2(self)
    

    enter image description here

    which is not what you want, so instead of creating a new you must pass the one that already exists, an option is to pass it through the constructor:

    class Tab1Widget1(QWidget):
        def __init__(self, bridge, parent=None): # Modify here
            super().__init__(parent)
            self.Tab1Widget1initUI()
            self.bridge = bridge  #Modify here
    
        # ...
    
        def test(self): print ('123')
    
        def run(self):
            if self.T1W1_checkbox.isChecked():
                self.test()
                if self.bridge.T1W2_checkbox.isChecked():
                    print (self.bridge.T1W2_le.text())
    
    class Tab1Widget2(QWidget):
        # ...
    
    class Tab1Layout(QWidget):
        def __init__(self, parent=None):
            super().__init__()
            self.setLayout(QGridLayout())
    
            self.group2 = Tab1Widget2(self) # Modify here
            self.group1 = Tab1Widget1(self.group2, self) # Modify here
            scroll = QScrollArea(self)
            scroll.setWidget(self.group1)
            scroll.setWidgetResizable(True)
            self.layout().addWidget(scroll, 0, 0)
    
            # self.group2 = Tab1Widget2(self) # Modify here
            self.layout().addWidget(self.group2, 1, 0)
    
            # ...