2017-10-12 82 views
0

我正在嘗試使源自SQLite3數據庫的動態Qcombobox(安排在Qtablewidget中)填充。底層數據(用於演示和簡單起見)可能是由以下代碼製作的表格中找到:動態QCombobox填充來自​​SQLite3的Qtablewidget DB

import sqlite3 

conn = sqlite3.connect('DataBase.db') 
c = conn.cursor() 

def create_table(): 
    c.execute('CREATE TABLE IF NOT EXISTS source("Section",\ 
               "Product_ID",\ 
               "Label",\ 
               "Product_desc",\ 
               "Unit_price")') 

list1 = [ 
    ['Butterfly','16/1/001','PP','Pepito Butterfly','350'], 
    ['Butterfly','16/1/002','PP','Brown Butterfly','350'], 
    ['Butterfly','16/1/003','PP','Blue Butterfly','350'], 
    ['Butterfly','bra01','BR','White Butterfly','500'], 
    ['Backpack','bra02','BR','Backpack-blue','1500'], 
    ['Backpack','bra03','BR','Backpack-black','1250'], 
    ['Toy','klv01','KL','Bear','200'], 
    ['Toy','klv02','KL','Fish','500'], 
    ['Toy','klv03','KL','Rabbit','400'], 
    ['Toy','klv04','KL','Owl','450'], 
    ] 

def data_entry(): 
    for element in list1: 
      c.execute("INSERT INTO source VALUES(?,?,?,?,?)", (element)) 
    conn.commit() 
    c.close() 
    conn.close() 

create_table() 
data_entry() 

我的目標是更新所有組合框(在同一行),並與更新的選擇方案填補他們每當用戶在任何組合框中選擇一些東西。的邏輯應以下:

Scenario1:COMBO1內 一個人選擇蝴蝶,如下combo2和combo3內選擇選項將被更新:combo2顯示三個選項(空白,PP,BR),並且默認設置爲空白, combo3將顯示(空白,Pepito蝴蝶,棕色蝴蝶,藍色蝴蝶,白色蝴蝶),默認設置爲空白,當用戶在combo2中選擇BR時,combo3的選擇選項將僅提供空白和白色Buttefly(設置爲空白默認)。

場景2: 一個在combo3中選擇揹包黑色,combo2的選擇選項將只是空白,BR(默認設置爲空白),combo1的選擇選項將只是空白,揹包(設置爲空白默認)。

場景3: 與場景1相同,但在第二種情況下(在選擇combo1中的蝴蝶後),用戶在combo3中選擇白色蝴蝶,combo2只應提供空白和BR(默認設置爲空白)。

空白值應作爲重新啓動以使用戶重置選擇選項。

在某些部分有一個類似於這個帖子的帖子,可能在這裏找到:Dynamic QComboBox fill dependent on user input PyQt5

在我的研究過程中,我發現了一些其他有用的帖子:sqlite3 table into QTableWidget, sqlite3, PyQt5,但是,我仍然沒有設法實現代碼片段,以便它能夠工作並決定直接使用sqlite3提取數據。

我被困在初始階段,在那裏我需要形成一個數據結構,在信號通過後(即進行選擇)將會被更新。這裏下面是QTableWidget的與Qcomboboxes我未能適當地源代碼:

進一步編輯 - 它已接近完成,所提供的選擇似乎是好的,但一個無法選擇他們出於某種原因:

import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
import sqlite3 
from pandas import DataFrame 

conn = sqlite3.connect('DataBase.db') 
c = conn.cursor() 
c.execute('select Section, Label, Product_desc from source') 
offer = c.fetchall() 
c.close() 
conn.close() 

df = DataFrame(offer) 
fin = {} 
for i in df: 
    fin[i] = df[i] 
    fin[i] = df[i].drop_duplicates() 
    fin[i] = list(fin[i]) 
    fin[i].insert(0,'') 

class Window(QMainWindow): 

    def __init__(self, parent = None): 
     super(Window,self).__init__(parent) 
     self.Table_of_widgets() 

    def Table_of_widgets(self): 

     rowCount = 20 
     columnCount = 9 

     self.table = QTableWidget() 
     self.table.setColumnCount(columnCount) 
     self.table.setRowCount(rowCount) 
     self.table.setHorizontalHeaderLabels(['Section', 'Label', 'Product description', 'Picture', 'Product ID', "Amount", "Unit price", "Store", "Total price"]) 
     self.table.verticalHeader().hide() 

     for i in range(columnCount): 
      self.table.horizontalHeader().setSectionResizeMode(i, QHeaderView.Stretch) 

     self.table.showMaximized() 

     self.offer1 = fin[0] 
     self.offer2 = fin[1] 
     self.offer3 = fin[2] 

     for i in range(rowCount): 
      comboA = QComboBox() 
      comboB = QComboBox() 
      comboC = QComboBox() 
      comboA.addItems(self.offer1) 
      comboB.addItems(self.offer2) 
      comboC.addItems(self.offer3) 
      self.table.setCellWidget(i, 0, comboA) 
      self.table.setCellWidget(i, 1, comboB) 
      self.table.setCellWidget(i, 2, comboC) 
      comboA.currentTextChanged.connect(lambda text1, row=i: self.onComboACurrentTextChanged(text1, row)) 
      comboB.currentTextChanged.connect(lambda text2, row=i: self.onComboBCurrentTextChanged(text2, row)) 
      comboC.currentTextChanged.connect(lambda text3, row=i: self.onComboCCurrentTextChanged(text3, row)) 

    def updateCombox(self, combo1, combo2, combo3, item1, item2, item3): 
     text1 = combo1.currentText() 
     text2 = combo2.currentText() 
     text3 = combo3.currentText() 
     combo1.blockSignals(True) 
     combo2.blockSignals(True) 
     combo3.blockSignals(True) 
     combo1.clear() 
     combo2.clear() 
     combo3.clear() 

     if text1 == '': a = list(df[0].drop_duplicates()) 
     else: a = [text1] 
     if text2 == '': b = list(df[1].drop_duplicates()) 
     else: b = [text2] 
     if text3 == '': c = list(df[2].drop_duplicates()) 
     else: c = [text3] 

     offer1 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][0].drop_duplicates()) 
     offer1.insert(0, ' ') 
     offer2 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][1].drop_duplicates()) 
     offer2.insert(0, ' ') 
     offer3 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][2].drop_duplicates()) 
     offer3.insert(0, ' ') 

     combo3.addItems(offer3) 
     combo3.setCurrentText(text3) 

     combo2.addItems(offer2) 
     combo2.setCurrentText(text2) 

     combo1.addItems(offer1) 
     combo1.setCurrentText(text1) 

     combo1.blockSignals(False) 
     combo2.blockSignals(False) 
     combo3.blockSignals(False) 

    def onComboACurrentTextChanged(self, text1, row): # Determines changes in given row iniciated by comboA 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

    def onComboBCurrentTextChanged(self, text2, row): # Determines changes in given row iniciated by comboB 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

    def onComboCCurrentTextChanged(self, text3, row): # Determines changes in given row iniciated by comboC 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

if __name__ == "__main__": 

    app = QApplication(sys.argv) 
    app.setApplicationName('MyWindow') 
    main = Window() 
    sys.exit(app.exec_()) 

我會感謝任何建議/解決方案/提示!由於

+0

一個查詢,你會增加組合框? :P – eyllanesc

+0

我並沒有計劃進一步增加組合框,它們只有三列(第三組決定獨特產品)。謝謝 – New2Python

+0

@ eyllanesc:我用動態數據框替換了你的字典,看起來很好,但是我沒有讓組合框內的選擇工作,有什麼建議嗎? – New2Python

回答

0

下面是我之後的代碼:

import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
import sqlite3 
from pandas import DataFrame 

conn = sqlite3.connect('DataBase.db') 
c = conn.cursor() 
c.execute('select Section, Label, Product_desc from source') 
offer = c.fetchall() 
c.close() 
conn.close() 

df = DataFrame(offer) 
fin = {} 
for i in df: 
    fin[i] = df[i] 
    fin[i] = df[i].drop_duplicates() 
    fin[i] = list(fin[i]) 
    fin[i].insert(0,' ') 

class Window(QMainWindow): 

    def __init__(self, parent = None): 
     super(Window,self).__init__(parent) 
     self.Table_of_widgets() 

    def Table_of_widgets(self): 

     rowCount = 20 
     columnCount = 9 

     self.table = QTableWidget() 
     self.table.setColumnCount(columnCount) 
     self.table.setRowCount(rowCount) 
     self.table.setHorizontalHeaderLabels(['Section', 'Label', 'Product description', 'Picture', 'Product ID', "Amount", "Unit price", "Store", "Total price"]) 
     self.table.verticalHeader().hide() 

     for i in range(columnCount): 
      self.table.horizontalHeader().setSectionResizeMode(i, QHeaderView.Stretch) 

     self.table.showMaximized() 

     self.offer1 = fin[0] 
     self.offer2 = fin[1] 
     self.offer3 = fin[2] 

     for i in range(rowCount): 
      comboA = QComboBox() 
      comboB = QComboBox() 
      comboC = QComboBox() 
      comboA.addItems(self.offer1) 
      comboB.addItems(self.offer2) 
      comboC.addItems(self.offer3) 
      self.table.setCellWidget(i, 0, comboA) 
      self.table.setCellWidget(i, 1, comboB) 
      self.table.setCellWidget(i, 2, comboC) 
      comboA.currentTextChanged.connect(lambda text, row=i: self.onComboACurrentTextChanged(text, row)) 
      comboB.currentTextChanged.connect(lambda text, row=i: self.onComboBCurrentTextChanged(text, row)) 
      comboC.currentTextChanged.connect(lambda text, row=i: self.onComboCCurrentTextChanged(text, row)) 

    def updateCombox(self, combo1, combo2, combo3, offer1, offer2, offer3): 
     text1 = combo1.currentText() 
     text2 = combo2.currentText() 
     text3 = combo3.currentText() 
     combo1.blockSignals(True) 
     combo2.blockSignals(True) 
     combo3.blockSignals(True) 
     combo1.clear() 
     combo2.clear() 
     combo3.clear() 

     if text1 == ' ': a = list(df[0].drop_duplicates()) 
     else: a = [text1] 
     if text2 == ' ': b = list(df[1].drop_duplicates()) 
     else: b = [text2] 
     if text3 == ' ': c = list(df[2].drop_duplicates()) 
     else: c = [text3] 

     offer1 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][0].drop_duplicates()) 
     offer1.insert(0, ' ') 
     offer2 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][1].drop_duplicates()) 
     offer2.insert(0, ' ') 
     offer3 = list(df.loc[df[0].isin(a) & df[1].isin(b) & df[2].isin(c)][2].drop_duplicates()) 
     offer3.insert(0, ' ') 

     combo3.addItems(offer3) 
     combo3.setCurrentText(text3) 

     combo2.addItems(offer2) 
     combo2.setCurrentText(text2) 

     combo1.addItems(offer1) 
     combo1.setCurrentText(text1) 

     combo1.blockSignals(False) 
     combo2.blockSignals(False) 
     combo3.blockSignals(False) 

    def onComboACurrentTextChanged(self, text, row): 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

    def onComboBCurrentTextChanged(self, text, row): 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

    def onComboCCurrentTextChanged(self, text, row): 
     comboA = self.table.cellWidget(row, 0) 
     comboB = self.table.cellWidget(row, 1) 
     comboC = self.table.cellWidget(row, 2) 
     self.updateCombox(comboA, comboB, comboC, self.offer1, self.offer2, self.offer3) 

if __name__ == "__main__": 

    app = QApplication(sys.argv) 
    app.setApplicationName('MyWindow') 
    main = Window() 
    sys.exit(app.exec_())