Для упрощения оставил кусок проги и маленькую таблицу из 2 столбцов: Locality.DB
Raion | Village
——–|———
Raion1|Village1
Raion1|Village2
Raion1|Village3
Raion2|Village1
Raion2|Village
Raion2|Village3
Использовал PYQT5. При выборе “Raion” в ComboBox_1, который заполнен в коде (combo.addItems(“Raion1”, “Raion2”,“Raion3”,“Raion4”,“Raion5”)), нужно чтобы ComboBox_3 заполнился данными (Village1, Village2, Village3) из базы согласно выбранному району.
Выходит ошибка:
self.initUI.combo_3.addItems(str(i))
AttributeError: ‘function’ object has no attribute 'combo_3
Код:
import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon, QFont from PyQt5.QtCore import QCoreApplication class MyWin(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): #comboBox1 self.lb8 = QLabel("ComboBox_1", self) self.lb8.move(30, 50) self.lbl = QLabel(self) self.lbl.move(250, 52) combo = QComboBox(self) combo.move(100, 50) combo.addItems([" ","Raion1", "Raion2","Raion3","Raion4","Raion5"]) combo.resize (130, 22) combo.activated[str].connect(self.onActivated) combo.activated[str].connect(self.selected) combo.activated[str].connect(self.combo3) #comboBox3 self.lb10 = QLabel("ComboBox_3", self) self.lb10.move(30, 90) self.lb3 = QLabel(self) self.lb3.move(250, 90) combo_3 = QComboBox(self) combo_3.move(100, 90) combo_3.resize (130, 22) combo_3.activated[str].connect(self.onActivated3) #Close button qbtn = QPushButton('Close', self) qbtn.clicked.connect(self.close) qbtn.resize(qbtn.sizeHint()) qbtn.move(450, 450) #Basic Window self.resize(600, 520) self.center() self.setWindowTitle('Locality') self.show() #qLabel_1 def onActivated(self, text): self.lbl.setText(text) self.lbl.adjustSize() #qLabel_3 def onActivated3(self, text): self.lb3.setText(text) self.lb3.adjustSize() #selected raion def selected(self, text): raion = text print (raion) def combo3(self, text): import sqlite3 raion = text conn = sqlite3.connect ("Locality.db") cursor = conn.cursor() cursor.execute ("""select Village from Locality where raion=? """,(raion,)) result = cursor.fetchall() combo3list = [row[0] for row in result] for row in combo3list: self.combo_3.addItems(row[0]) #main window def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def closeEvent(self, event): reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() if __name__ == '__main__': app = QApplication(sys.argv) my = MyWin() sys.exit(app.exec_())