Search code examples
arraystkinterappendtuplesfetchall

appending tuple list from one to another


I am creating a UI where I grab a set of data from an SQL database file and inserting it to a Listbox. I am using Tkinter framework. I'm having an issue where I want to append the tuples onto another empty array so I can append a limited amount of data with a set delay. The code I have shown, is what I have created so far, but I am receiving the error below and I have no clue on how to fix it. I'm also not too familiar working with tuples.

    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)

Error:

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

Solution

  • Your problem can be simplified as below:

    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)
    

    The simple solution is not to use the name counter in your for loop of list1:

    for item in list1:
        print(item)