[Python] I have been able to load a QCombobox from sqlite3 db but unable to load a dependent...

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    from PyQt5.QtCore import QDate, Qt
    from PyQt5.QtWidgets import (QLabel, QLineEdit,
    QPushButton, QDateEdit,
    QWidget, QHBoxLayout, QComboBox,
    QVBoxLayout,)
    import sqlite3


    class Customer(QWidget):
    def __init__(self, menu): # Customer is able to refer to menu through the init
    function
    super().__init__()
    self.menu = menu
    self.combo1 = QComboBox()
    self.combo1.currentIndexChanged.connect(self.fill_cb2)
    self.combo2 = QComboBox()
    self.initUI()
    self.fill_cb1()

    def initUI(self):
    self.setWindowTitle("Customer Information")
    # Set the window size
    self.setFixedSize(600, 300)

    # MainLayout
    main_layout = QVBoxLayout()
    main_layout.setContentsMargins(30, 10, 30, 10) # (left_side, top_side, right_side,
    bottom_side)

    # Create a Layout
    layout1 = QHBoxLayout()
    layout1.setContentsMargins(0, 20, 0, 20) # (left_side, top_side, right_side,
    bottom_side)
    # Create labels and Input Boxes for layout
    label1 = QLabel("Tracking | WO No:")
    label1.setStyleSheet("font-size: 16px;") # Set font size for the label
    label1.setAlignment(Qt.AlignCenter) # Center the label
    layout1.addWidget(label1)

    text1 = QLineEdit()
    text1.setStyleSheet("font-size: 16px;")
    text1.setFixedSize(150, 26)
    # text1.setAlignment(Qt.AlignCenter)
    layout1.addWidget(text1)

    # Add a blank space (spacer)
    layout1.addStretch() # This will create a blank space that expands

    label2 = QLabel("Date:")
    label2.setStyleSheet("font-size: 16px;") # Set font size for the label
    label2.setAlignment(Qt.AlignCenter) # Center the label
    layout1.addWidget(label2)

    text2 = QDateEdit()
    text2.setStyleSheet("font-size: 16px;")
    text2.setAlignment(Qt.AlignCenter)
    now = QDate.currentDate()
    text2.setDate(now)
    layout1.addWidget(text2)

    # Adding the comboboxes
    layout2 = QVBoxLayout()
    layout2.setContentsMargins(0, 0, 0, 30) # (left_side, top_side, right_side,
    bottom_side)

    label3 = QLabel("Customer Name:")
    label3.setStyleSheet("font-size: 16px;") # Set font size for the label
    label3.setAlignment(Qt.AlignLeft) # Center the label
    layout2.addWidget(label3)

    # self.combo1 = QComboBox()
    # self.combo1.currentIndexChanged.connect(self.fill_cb2)
    self.combo1.setStyleSheet("font-size: 20px;")
    self.combo1.setFixedSize(540, 30)

    layout2.addWidget(self.combo1)

    # Add a blank space (spacer)
    layout2.addStretch(30) # This will create a blank space that expands

    label4 = QLabel("Customer Address:")
    label4.setStyleSheet("font-size: 16px;") # Set font size for the label
    label4.setAlignment(Qt.AlignLeft) # Center the label
    layout2.addWidget(label4)

    # self.combo2 = QComboBox()
    self.combo2.setStyleSheet("font-size: 20px;")
    self.combo2.setFixedSize(540, 30)
    layout2.addWidget(self.combo2)

    # Adding the buttons
    layout3 = QHBoxLayout()

    # Add a blank space (spacer)
    layout3.addStretch(30) # This will create a blank space that expands

    btn1 = QPushButton("Next")
    btn1.setStyleSheet("font-size: 16px;") # Set font size for the button
    btn1.setFixedSize(125, 40) # Set fixed size for button1

    btn2 = QPushButton("Back")
    btn2.setStyleSheet("font-size: 16px;") # Set font size for the button
    btn2.setFixedSize(125, 40) # Set fixed size for button2
    btn2.clicked.connect(lambda: self.return2menu())

    layout3.addWidget(btn2)
    layout3.addWidget(btn1)

    # Add all layouts to the Main Layout
    main_layout.addLayout(layout1)
    main_layout.addLayout(layout2)
    main_layout.addLayout(layout3)

    # Set the main layout to the screen
    self.setLayout(main_layout)



    def return2menu(self):
    self.hide()
    self.menu.show()

    def open_issue(self):
    self.hide()

    def fill_cb1(self):
    conn = sqlite3.connect('my_service.db')
    cur = conn.cursor()

    query = "SELECT DISTINCT tick_cust FROM ticket ORDER BY tick_cust;"

    results = cur.execute(query)

    self.combo1.clear()
    for result in results:
    self.combo1.addItem(result[0])

    self.combo1.setCurrentIndex(-1)
    conn.close()



    def fill_cb2(self):
    self.combo2.clear()
    value1 = self.combo1.currentText()
    print(f'{value1}')
    conn = sqlite3.connect('my_service.db')
    cur = conn.cursor()

    query = "SELECT DISTINCT tick_addr FROM ticket WHERE tick_cust = '%s' ORDER BY
    tick_addr;"

    cur.execute(query, (value1,))

    results = cur.fetchall()

    self.combo2.clear()
    for result in results:
    print(f'{result[0]}')
    self.combo2.addItem(result[0])

    self.combo2.setCurrentIndex(-1)
    conn.close()

    Continue reading...

Compartilhe esta Página