Python Forum
[PyQt] making dependant comboBoxes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] making dependant comboBoxes
#1
Hello,i want to make a dependant combo boxes,where i basically choose 1 option from combobox1 and that option has its own set of options in combobox2. here,when i choose "Roman" from combobox1,i want it to populate other combobox with my already made list.
i keep searching and even though there are some explanations,i can't seem to make it work in my code.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.setEnabled(True)
        Form.resize(471, 392)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("iconfinder_Bleach_197477.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.label_author = QtWidgets.QLabel(Form)
        self.label_author.setEnabled(True)
        self.label_author.setGeometry(QtCore.QRect(340, 355, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Nimbus Mono L")
        font.setPointSize(9)
        self.label_author.setFont(font)
        self.label_author.setObjectName("label_author")
        self.label_1 = QtWidgets.QLabel(Form)
        self.label_1.setGeometry(QtCore.QRect(12, 42, 50, 22))
        self.label_1.setObjectName("label_1")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(12, 78, 34, 22))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(12, 114, 34, 22))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(12, 150, 50, 22))
        self.label_4.setObjectName("label_4")
        self.lineEdit_1 = QtWidgets.QLineEdit(Form)
        self.lineEdit_1.setGeometry(QtCore.QRect(68, 42, 142, 30))
        self.lineEdit_1.setObjectName("lineEdit_1")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(68, 78, 142, 30))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Form)
        self.lineEdit_3.setGeometry(QtCore.QRect(68, 114, 142, 30))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_4 = QtWidgets.QLineEdit(Form)
        self.lineEdit_4.setGeometry(QtCore.QRect(68, 150, 142, 30))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(10, 360, 91, 22))
        font = QtGui.QFont()
        font.setFamily("FreeMono")
        font.setBold(False)
        font.setItalic(True)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.line = QtWidgets.QFrame(Form)
        self.line.setGeometry(QtCore.QRect(11, 187, 227, 16))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(140, 355, 161, 31))
        self.pushButton.setObjectName("pushButton")
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(10, 211, 43, 31))
        self.label_5.setObjectName("label_5")
        self.lineEdit_5 = QtWidgets.QLineEdit(Form)
        self.lineEdit_5.setGeometry(QtCore.QRect(70, 210, 141, 30))
        self.lineEdit_5.setReadOnly(True)
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.line_2 = QtWidgets.QFrame(Form)
        self.line_2.setGeometry(QtCore.QRect(10, 260, 227, 16))
        self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.line_3 = QtWidgets.QFrame(Form)
        self.line_3.setEnabled(True)
        self.line_3.setGeometry(QtCore.QRect(230, 10, 16, 258))
        self.line_3.setLineWidth(1)
        self.line_3.setMidLineWidth(0)
        self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.comboBox_2 = QtWidgets.QComboBox(Form)
        self.comboBox_2.setGeometry(QtCore.QRect(360, 40, 86, 30))
        self.comboBox_2.setObjectName("comboBox")
        self.comboBox = QtWidgets.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(250, 40, 86, 30))
        self.comboBox.setObjectName("comboBox_2")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(260, 10, 68, 22))
        self.label_6.setAlignment(QtCore.Qt.AlignCenter)
        self.label_6.setObjectName("label_6")
        self.label_7 = QtWidgets.QLabel(Form)
        self.label_7.setGeometry(QtCore.QRect(370, 10, 68, 22))
        self.label_7.setAlignment(QtCore.Qt.AlignCenter)
        self.label_7.setWordWrap(False)
        self.label_7.setObjectName("label_7")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "UNI-TRAV Calculator"))
        self.label_author.setText(_translate("Form", "Version:  1.0.0"))
        self.label_1.setText(_translate("Form", "Wood: "))
        self.label_2.setText(_translate("Form", "Clay:"))
        self.label_3.setText(_translate("Form", "Iron:"))
        self.label_4.setText(_translate("Form", "Wheat:"))
        self.label.setText(_translate("Form", "Hitsugaya"))
        self.pushButton.setText(_translate("Form", "Magic Button"))
        self.label_5.setText(_translate("Form", "Total: "))
        self.lineEdit_5.setText(_translate("Form", "0"))
        self.comboBox.setItemText(0, _translate("Form", "Romans"))
        self.comboBox.setItemText(1, _translate("Form", "Gauls"))
        self.comboBox.setItemText(2, _translate("Form", "Teutons"))
        self.label_6.setText(_translate("Form", "Tribe:"))
        self.label_7.setText(_translate("Form", "Unit:"))


        self.pushButton.clicked.connect(self.klik)

        Romans = ["Leggionaire", "Praetorian", "Imperian", "Equites Legati", "Equites Imperatoris",
         "Equites Caesaris", "Battering Ram", "Fire Catapult", "Senator", "Settler"]

        Gauls = ["Phalanx", "Swordsman", "Pathfinder", "Theutates Thunder", "Druidrider", "Haeduan",
         "Ram", "Trebuchet", "Chieftain", "Settler"]

        Teutons = ["Clubswinger", "Spearman", "Axeman", "Scout", "Paladin", "Teutonic Knight", "Ram",
         "Catapult", "Chief", "Settler"]


    def klik(self):
        textbox1 = self.lineEdit_1.text()
        textbox2 = self.lineEdit_2.text()
        textbox3 = self.lineEdit_3.text()
        textbox4 = self.lineEdit_4.text()
        self.lineEdit_5.setText(str(int(textbox1) + int(textbox2) + int(textbox3) + int(textbox4)))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
Reply
#2
#!/usr/bin/python3
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Main(QtWidgets.QMainWindow):
    def __init__(self, parent):
        super().__init__()
        self.ui = QtWidgets.QWidget(self)
        self.setCentralWidget(self.ui)
        self.ui.combo1 = QtWidgets.QComboBox()
        self.ui.combo2 = QtWidgets.QComboBox()

        self.ui.combo1.currentTextChanged.connect(self._updateCombo2)
        self.ui.combo1.addItems(("A", "B", "C"))

        self.ui.layout = QtWidgets.QVBoxLayout()
        self.ui.layout.addWidget(self.ui.combo1)
        self.ui.layout.addWidget(self.ui.combo2)
        self.ui.setLayout(self.ui.layout)
        self.show()

    def _updateCombo2(self, text):
        self.ui.combo2.clear()
        if text == "A":
            self.ui.combo2.addItems(("1", "2", "3"))
        elif text == "B":
            self.ui.combo2.addItems(("4", "5", "6"))
        elif text == "C":
            self.ui.combo2.addItems(("7", "8", "9"))

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main(app)
    sys.exit(app.exec_())
Reply
#3
it worked great,i implemented your example.
Reply
#4
I'm glad it helped :)

In the future, it is a good habit to refer to the Qt documentation. There you can find all the information you need about the methods and signals that are available for the widget you are working with.

In this case: https://doc.qt.io/qt-5/qcombobox.html#signals
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,622 Jan-11-2022, 08:37 PM
Last Post: deanhystad
  Linking Comboboxes MrP 24 7,118 Feb-03-2021, 10:59 PM
Last Post: MrP
  Print Values from a Sequence of Entries / ComboBoxes MC2020 4 2,800 Mar-28-2020, 10:05 PM
Last Post: MC2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020