PyQt5为每个组合框选项提供了不同的复选框集

时间:2019-03-14 11:27:35

标签: python python-3.x pyqt pyqt5

我正尝试使GUI应用程序根据组合框的先前选择显示不同的复选框,例如,如果用户在组合框内选择“ IT”,则将向他/她显示a,b,c复选框,如果用户选择了其他选项,则会向他/她显示x,y,z。

这是我的代码,其中包含一些可能有用的注释:

import sys
from PyQt5 import QtWidgets, QtGui

class MainWindow:
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.window = QtWidgets.QMainWindow()

        self.departmentLabel()
        self.departmentComboBox()
        self.tracksLabel()

        self.window.show()
        self.window.setGeometry(600,200,600,600)


        sys.exit(self.app.exec_())


    def departmentLabel(self):
        self.selectDepartmentlabel = QtWidgets.QLabel("<h3>Select department: </h3>", self.window)
        self.selectDepartmentlabel.setGeometry(20,20,300,30)

    def departmentComboBox(self):
        self.departmentComboBox = QtWidgets.QComboBox(self.window)
        self.departmentComboBox.addItem('IT')
        self.departmentComboBox.addItem('Business')
        self.departmentComboBox.move(450,20)
        self.departmentComboBox.activated[str].connect(self.tracksCheckBox)

    def tracksLabel(self):
        self.selectTracksLabel = QtWidgets.QLabel('<h3>Select tracks: </h3>', self.window)
        self.selectTracksLabel.setGeometry(20,60,300,30)


    def tracksCheckBox(self):
        track = self.departmentComboBox.currentText()
        self.testCheckBox = QtWidgets.QLineEdit('Testing', self.window) # Does not execute.
        self.window.show()
        if (track == "IT"):
            print("Condition is met") # oddly enough, this executes normally.
            self.itcCheckBox = QtWidgets.QCheckBox('ITC', self.window) # Does not execute.
            self.itcCheckBox.move(20,100)
            self.nsCheckBox = QtWidgets.QCheckBox('NS', self.window) # Does not execute.
            self.nsCheckBox.move(80,100)
            self.csCheckBox = QtWidgets.QCheckBox('CS', self.window) # Does not execute.
            self.csCheckBox.move(140,100)

main = MainWindow()

1 个答案:

答案 0 :(得分:0)

如果不更改应用程序的逻辑和设计,它可能看起来像这样:

import sys
from PyQt5 import QtWidgets, QtGui

class MainWindow:
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.window = QtWidgets.QMainWindow()

        self.departmentLabel()
        self.departmentComboBox()
        self.tracksLabel()

        self.window.show()
        self.window.setGeometry(600,100,600,600)

        self.itcCheckBox = None      # +++
        self.nsCheckBox  = None      # +++
        self.csCheckBox  = None      # +++

        sys.exit(self.app.exec_())


    def departmentLabel(self):
        self.selectDepartmentlabel = QtWidgets.QLabel("<h3>Select department: </h3>", self.window)
        self.selectDepartmentlabel.setGeometry(20,20,300,30)

    def departmentComboBox(self):
        self.departmentComboBox = QtWidgets.QComboBox(self.window)
        self.departmentComboBox.addItem('IT')
        self.departmentComboBox.addItem('Business')
        self.departmentComboBox.move(450,20)
        self.departmentComboBox.activated[str].connect(self.tracksCheckBox)

    def tracksLabel(self):
        self.selectTracksLabel = QtWidgets.QLabel('<h3>Select tracks: </h3>', self.window)
        self.selectTracksLabel.setGeometry(20,60,300,30)


    def tracksCheckBox(self, text):   # + text
        print(text, " <---> ", self.departmentComboBox.currentText())
        track = self.departmentComboBox.currentText()
        self.testCheckBox = QtWidgets.QLineEdit('Testing '+text, self.window) # Does not execute.

#        self.window.show()                           # ---
        self.testCheckBox.setGeometry(450,60,100,30)  # +++
        self.testCheckBox.show()                      # +++

        if self.itcCheckBox and self.nsCheckBox and self.csCheckBox: # +++
           self.itcCheckBox.deleteLater()
           self.itcCheckBox.hide()
           self.nsCheckBox.deleteLater()
           self.nsCheckBox.hide()
           self.csCheckBox.deleteLater()
           self.csCheckBox.hide()

        if (track == "IT"):
            print("Condition is met") # oddly enough, this executes normally.
            self.itcCheckBox = QtWidgets.QCheckBox('ITC', self.window) # Does not execute.
            self.itcCheckBox.move(20,100)
            self.nsCheckBox = QtWidgets.QCheckBox('NS', self.window) # Does not execute.
            self.nsCheckBox.move(80,100)
            self.csCheckBox = QtWidgets.QCheckBox('CS', self.window) # Does not execute.
            self.csCheckBox.move(140,100)
# +++ vvvvvvvvvvvvvvvvvvvvvv          
        else:
            self.itcCheckBox = QtWidgets.QCheckBox('XXXX', self.window) # Does not execute.
            self.itcCheckBox.move(20,100)
            self.nsCheckBox = QtWidgets.QCheckBox('Yyyy', self.window) # Does not execute.
            self.nsCheckBox.move(80,100)
            self.csCheckBox = QtWidgets.QCheckBox('Zzzz', self.window) # Does not execute.
            self.csCheckBox.move(140,100)        

        self.itcCheckBox.show()
        self.nsCheckBox.show()
        self.csCheckBox.show()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^

main = MainWindow()

enter image description here