I am making a basic contacts app with PyQt5 and SQL. Once the app opened, I want to show all the contacts on the screen. So I want to add a few widgets to the UI for every contact but I don't know how many of them will be added at start (since the user may change the amount of contacts with adding or deleting a contact) so I can't add them manually. I tried it with a for loop but since the objects' names must be diffrent from each other, it didn't worked.
Here's the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class Contact:
def __init__(self, id, first_name, last_name, phone_numbers, note = ""):
self.id = id
self.first_name = first_name
self.last_name = last_name
self.phone_numbers = phone_numbers
self.note = note
def getBoxInfo(self):
return f"{self.first_name}, {self.last_name}"
def getAllInfo(self):
if self.note != "":
return f"{self.first_name}, {self.last_name}, {self.phone_numbers}, {self.note}"
else:
return f"{self.first_name}, {self.last_name}, {self.phone_numbers}, "
#setting the connection to the database
connection = sqlite3.connect('contacts.db')
cursor = connection.cursor()
#getting 'user' and 'phone_number' datas from the database
cursor.execute("SELECT * FROM users")
connection.commit()
Users = cursor.fetchall()
cursor.execute("SELECT * FROM phone_numbers")
connection.commit()
PhoneNumbers = cursor.fetchall()
#Putting together the 'user' and 'phone_number' datas in the 'Contact' class subject and adding them to an array
Contacts = []
for i in range(len(Users)):
newContact = Contact(Users[i][0], Users[i][1], Users[i][2], list(), Users[i][3])
if newContact.note == None:
newContact.note = ""
for j in PhoneNumbers:
if j[1] == Users[i][0]:
newContact.phone_numbers.append(j[2])
Contacts.append(newContact)
#Creating the UI
class Ui_MainWindow(object):
def setupUi(self, MainWindow, Contacts):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1200, 675)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#The part I tried to create widgets as many as 'users'
for i in range(len(Contacts)):
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(30, 30, 241, 81))
self.widget.setObjectName(f"widget{i}")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setGeometry(QtCore.QRect(150, 20, 75, 23))
self.pushButton.setObjectName(f"pushButton{i}")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 816, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow, Contacts)
MainWindow.show()
sys.exit(app.exec_())