尝试在Python中加载两个自动完成csv数据库(一个gui)。可能吗?每次尝试都会出错

2024-09-27 02:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我代码的底部:

    import sys
    app  = QtWidgets.QApplication(sys.argv)
    w = Window(autocompleteList3, autocompleteList2)
    w.show()

我需要能够同时导入autocompleteList3和autocompleteList2。然而,当我这样做时,我得到了“TypeError:init()接受2个位置参数,但给出了3个。”它只允许我单独使用autocompleteList3或autocompleteList2

下面是我的自动完成功能(如果有帮助):

class Window(QtWidgets.QMainWindow, Ui_MainWindow):        
    def __init__(self, cList):
        super().__init__()
        self.setupUi(self)                                 
        self.cList = cList
        self.Input_5.addItems(sorted(cList.keys())) 
        self.Input_5.activated[str].connect(self.onActivatedText)


class Window2(QtWidgets.QMainWindow, Ui_MainWindow):        
    def __init__(self, cList):
        super().__init__()
        self.setupUi(self)                                 
        self.cList = cList
        self.Input_7.addItems(sorted(cList.keys())) 
        self.Input_7.activated[str].connect(self.onActivatedText2)

    @QtCore.pyqtSlot(str)
    def onActivatedText(self, text):
        self.Input_19.setText(self.cList[text][0])
        self.Input_29.setText(self.cList[text][1])
        self.Input_30.setText(self.cList[text][2])
        self.Input_18.setText(self.cList[text][3])

    @QtCore.pyqtSlot(str)
    def onActivatedText2(self, text):
        self.Input_19.setText(self.cList[text][0])
        self.Input_29.setText(self.cList[text][1])
        self.Input_30.setText(self.cList[text][2])
        self.Input_18.setText(self.cList[text][3])

最后,这里是autocompleteList3(2看起来相同):

autocompleteList3 = {
'': ['','','','','','','','','','','','','','','','','',''],
    'two': ['3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', 'test55'],
    'one': ['3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', 'test55'],
}


with open('boxes.csv', mode='r') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        autocompleteList2[row[0]] = [row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]]


有人知道怎么做吗?我需要在同一窗口中具有两个从csv文件提取的自动完成功能。:/


Tags: csvtextselfinputinitdefreaderrow
1条回答
网友
1楼 · 发布于 2024-09-27 02:21:32

我明白了。见下文

我不得不离开cList,将autocompleteList2分配到它的位置。工作完美。“尝试例外”通行证用于在处理空白单元格时防止崩溃

类窗口(QtWidgets.QMainWindow,Ui_MainWindow):
definit(自我,客户端): super().init()

    self.setupUi(self)                                 

    self.cList = cList
    self.Input_5.addItems((cList.keys())) 
    self.Input_5.activated[str].connect(self.onActivatedText2)

    self.autocompleteList2 = autocompleteList2
    self.Input_7.addItems((autocompleteList2.keys())) 
    self.Input_7.activated[str].connect(self.onActivatedText)

@QtCore.pyqtSlot(str)
def onActivatedText2(self, text):
    try:
        self.Input_19.setText(self.cList[text][0])
        self.Input_29.setText(self.cList[text][1])
        self.Input_30.setText(self.cList[text][2])
        self.Input_18.setText(self.cList[text][3])
        self.Input_15.setText(self.cList[text][4])
        self.Input_14.setText(self.cList[text][5])
        self.Input_13.setText(self.cList[text][6])
        self.Input_25.setText(self.cList[text][7])
        self.Input_20.setText(self.cList[text][8])
        self.Input_21.setText(self.cList[text][9])
        self.Input_22.setText(self.cList[text][10])
        self.Input_24.setText(self.cList[text][11])
        self.Input_26.setText(self.cList[text][12])
        self.Input_28.setText(self.cList[text][13])
        self.Input_23.setText(self.cList[text][14])
        self.Input_27.setText(self.cList[text][15])
        self.Input_31.setText(self.cList[text][16])
        self.Input_32.setText(self.cList[text][17])
        self.Input_33.setText(self.cList[text][18])
    except: pass

@QtCore.pyqtSlot(str)
def onActivatedText(self, text):
    try:
        self.Input_6.setText(self.autocompleteList2[text][0])
        self.Input_8.setText(self.autocompleteList2[text][1])
        self.Input_9.setText(self.autocompleteList2[text][2])
    except: pass

相关问题 更多 >

    热门问题