将元组列表从一个追加到另一个

问题描述 投票:0回答:1

我正在创建一个UI,从SQL数据库文件中获取一组数据并将其插入到列表框。我正在使用Tkinter框架。我遇到了一个问题,我想将元组附加到另​​一个空数组上,以便可以按设置的延迟附加有限数量的数据。到目前为止,我已经显示了所显示的代码,但是我收到下面的错误,并且不知道如何解决。我对元组的工作也不是很熟悉。

    from tkinter import *
    import sqlite3 as sq
    import time

    def Record():
        conn = sq.connect('brian_UI_test.db')
        c = conn.cursor()
        c.execute("SELECT*FROM TEST")

       rows = c.fetchall() # Gets the data from the table

    counter = 0
    list1 = []
    for row in rows:
        counter = counter + 1
        Lb.insert(END,row)# Inserts record row by row in list box
        list1.append(row)
        print(row)
        time.sleep(1)
        if counter%4==0:
           for counter in list1:
            print(counter)
            Lb.insert(counter)



    c.close()
    conn.close()

    window = Tk()
    frame = Frame(window)

    Lb = Listbox(frame, height = 8, width = 25,font=("arial", 12)) 

    scroll = Scrollbar(frame, orient = VERTICAL) # set scrollbar to listbox for when entries exceed size of list box
    scroll.config(command = Lb.yview)

    scroll1 = Scrollbar(frame, orient = HORIZONTAL)
    scroll1.config(command = Lb.xview)

    Lb.config(yscrollcommand = scroll.set)
    Lb.config(xscrollcommand = scroll1.set) 

    scroll1.pack(side = BOTTOM, fill = X)
    Lb.pack(side = LEFT, fill = Y)
    scroll.pack(side = RIGHT, fill = Y)

    Lb.insert(0, 'Time,   Message') #first row in listbox

    b13 = Button(window, text = "OPEN DB File", width= 14, command=lambda:Record())
    b13.grid(row = 0, column = 7, padx = 10)

错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\Python37_64\lib\tkinter\__init__.py",line 1705, in __call__return self.func(*args)
File "Interface.py", line 122, in <lambda>
b13 = Button(window, text = "OPEN DB File", width= 14, command=lambda:Record())
File "Interface.py", line 25, in Record
counter = counter + 1
TypeError: can only concatenate tuple (not "int") to tuple
arrays tkinter append tuples fetchall
1个回答
0
投票

您的问题可以如下简化:

counter = 0
list1 = []
rows = [(1,2),(3,4,),(5,6,),(7,8),(9,10)]

for row in rows:
    counter = counter + 1 #after the first loop, counter is a tuple and cannot concatenate with an int
    list1.append(row)
    if counter % 4 == 0:
        for counter in list1: #you redefined counter here which is a tuple
            print(counter)

简单的解决方案是不在list1的counter循环中使用名称for

for item in list1:
    print(item)
© www.soinside.com 2019 - 2024. All rights reserved.