Python Forum
[PyQt] Updating combobox value from database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Updating combobox value from database
#1
Hello,
Im beginner in in Ptyhon and pyQT5. I've created simple interface uing Qt Designer. Combobox contains list of groups selected from mySQL database. Clicked button opens new window with edit field where user can write new group name and button. Clicking button adds new group to database, but added position doesn't display in combobox until i restart program.

There is 2 files
main:
from PyQt5 import QtWidgets
from window.itpConnect import Ui_MainWindow 
from PyQt5.QtWidgets import *
import pymysql
import Qt
import subprocess
import sys
import os
from config import connData_LAN,connData_WAN,checkHost
  
MySQL_LAN = connData_LAN()
MySQL_WAN = connData_WAN()
 
class mywindow(QtWidgets.QMainWindow,object):
 
    def __init__(self):
        super(mywindow, self).__init__()
         #odwolanie do bazy danych
        if checkHost(MySQL_LAN[0], MySQL_LAN[4]):
            print("connected dbLAN")
            self.db = pymysql.connect(MySQL_LAN[0],MySQL_LAN[1],MySQL_LAN[2], MySQL_LAN[3])
            self.cursor = self.db.cursor()
        elif checkHost(MySQL_WAN[0], MySQL_WAN[4]):
            print("connected dbWAN")
            self.db = pymysql.connect(MySQL_WAN[0],MySQL_WAN[1],MySQL_WAN[2], MySQL_WAN[3])
            self.cursor = self.db.cursor()
        else:
            print("********** Nie mogę połączyć się do bazy danych! LAN i WAN False\n\n\n\n")
        #koniec odwolanie do bazy danych
 
 
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.actionKoniec.triggered.connect(self.koniec)
        self.ui.actionDodaj_grup.triggered.connect(self.dodajGrupe)
        self.ui.pushButton_Koniec.clicked.connect(self.koniec)
        self.ui.pushButton_Grupy.clicked.connect(self.dodajGrupe)
        self.listaKlientow()
  
    def dodajGrupe(self):
        from dodajGrupe import DodajNowaGrupe # dodaj nowa grupe
        self.oknoNowaGrupa = DodajNowaGrupe()
        self.oknoNowaGrupa.show()
  
    def listaKlientow(self):
        sql = "SELECT nazwa from grupy"
        self.cursor.execute(sql)
        results = self.cursor.fetchall()
        self.ui.comboBox.clear()
        self.ui.comboBox.addItem("--- Wybierz klienta ---")
        for row in results:
            nazwaGrupy = row[0]
            self.ui.comboBox.addItem(nazwaGrupy)
            print(nazwaGrupy)
  
    def koniec(self):
        self.close()
  
if __name__ == '__main__': 
    try:
        app
    except:    
        app = QtWidgets.QApplication([])
        application = mywindow()
        application.show()
        sys.exit(app.exec())
 
and opening windows where i can add new group
from PyQt5 import QtWidgets
from window.nowaGrupa import Ui_nowaGrupa #okno dodaj nowa grupe
from PyQt5.QtWidgets import *
import pymysql
import subprocess
import sys
import os
import base64
from config import connData_LAN,connData_WAN,checkHost
 
MySQL_LAN = connData_LAN()
MySQL_WAN = connData_WAN()
 
class DodajNowaGrupe(QtWidgets.QMainWindow):
 
    def __init__(self):
        super(DodajNowaGrupe, self).__init__()
        self.ui2 = Ui_nowaGrupa()
        self.ui2.setupUi(self)
 
        #odwolanie do bazy danych
        if checkHost(MySQL_LAN[0], MySQL_LAN[4]):
            print("connected dbLAN")
            self.db = pymysql.connect(MySQL_LAN[0],MySQL_LAN[1],MySQL_LAN[2], MySQL_LAN[3])
            self.cursor = self.db.cursor()
        elif checkHost(MySQL_WAN[0], MySQL_WAN[4]):
            print("connected dbWAN")
            self.db = pymysql.connect(MySQL_WAN[0],MySQL_WAN[1],MySQL_WAN[2], MySQL_WAN[3])
            self.cursor = self.db.cursor()
        else:
            print("********** Nie mogę połączyć się do bazy danych! LAN i WAN False\n\n\n\n")
        #koniec odwolanie do bazy danych
 
        self.ui2.pushButton_zamknij.clicked.connect(self.koniec) #przycisk koniec   
        self.ui2.pushButton_zapisz.clicked.connect(self.zapiszDane) #przycisk zapisz
 
 
    def zapiszDane(self):
        nowaGrupaTxt = self.ui2.lineEdit_nazwaGrupy.text()
        nowaGrupa = str(nowaGrupaTxt.strip())
        opisGrupy = self.ui2.textEdit_opis.toPlainText()
 
        self.cursor.execute(
            "SELECT nazwa from grupy WHERE nazwa = %s",
            (nowaGrupa,)
        )
        row_count = self.cursor.rowcount
        results = self.cursor.fetchall()
        for row in results:
            grupaZBazyDanych = row[0]
        if row_count == 0:
            grupaZBazyDanych = ''
  
        if not nowaGrupa or nowaGrupa.lower() == grupaZBazyDanych.lower():
            QMessageBox.about(self, "Błąd, dodawania nowej grupy", "Grupa o takiej nazwie już istnieje lub pole 'nazwa grupy' jest puste.")
        else:
            sql = "INSERT INTO grupy (nazwa,opis) VALUES (%s,%s)"
            addNewGroup = (nowaGrupa, opisGrupy)
            self.cursor.execute(sql,addNewGroup)
            self.db.commit()
            self.close()
            QMessageBox.about(self, "Dodano nową grupę", "Nowa grupa został dodana!")
 
            import main
            main.mywindow().listaKlientow()
  
    def koniec(self):
        self.close()
 
if __name__ == '__main__': 
    try:
        app1
    except:    
        app1 = QtWidgets.QApplication([])
        application = DodajNowaGrupe()
        application.show()
        sys.exit(app1.exec())
  
After grup added i've printing what should be added in loop for test, and i see it in console, but new position in combobox doesn't appear.
Sorry for my english and I will appreciate all the hints.
Reply
#2
Have you done anything to verify that the listaKlientow() method is called again after you add the group?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,329 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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