0

I have written code to show data from a sql query in tkinter. From a search function I want to print out the first item in the tuple on one line, then the rest of the results on another. I know that every tuple in the tuple has four data items (date, name , start time, end time), but I cant figure out how to get it to work properly.

def searched():
    Main_Screen.configure(state = 'normal')
    Main_Screen.delete('1.0', END)
    searched_for = search.get()
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = (?)''',(searched_for,))
    list1 = list(cursor.fetchall())
    print(list1)
    cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = (?) ''',(searched_for,))
    result = cursor.fetchone()[0]
    print(result)
    n = 1
    m = 1
    for i in range(0, result - 1):
        my_variable = list1[m-1][n-1]
        Main_Screen.insert(tkinter.END,my_variable)
        Main_Screen.insert(tkinter.END, "\n")
        for i in range(0,3):
            my_var2 = list1[m-1][2-1]
            Main_Screen.insert(tkinter.END,my_var2)

            my_var3 = list1[m-1][3-1]
            Main_Screen.insert(tkinter.END,my_var3)

            my_var4 = list1[m-1][4-1]
            Main_Screen.insert(tkinter.END,my_var3)

            Main_Screen.insert(tkinter.END, "\n")
            m = m + 1

For example I search for everything with the name 'test'. The list I receive is:

[('2017-07-14', 'test', '11:00:00', '13:00:00'), ('2017-07-21', 'test', '11:00:00', '13:00:00'), ('2017-07-28', 'test', '11:00:00', '13:00:00'), ('2017-08-04', 'test', '11:00:00', '13:00:00'), ('2017-08-11', 'test', '11:00:00', '13:00:00'), ('2017-03-10', 'test', '12:00:00', '14:00:00')]

6(how many are in the main tuple)

but when it prints to the gui:

2017-07-14
test11:00:0011:00:00
test11:00:0011:00:00
test11:00:0011:00:00
2017-08-04
test11:00:0011:00:00
test11:00:0011:00:00
test12:00:0012:00:00

What I want:

Date of timeslot
Name of Timeslot
Start Time
End Time

Also for some reason I get this error:

  File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 124, in searched
my_variable = list1[m-1][n-1]
IndexError: list index out of range

EDIT=

def searched():
    Main_Screen.configure(state = 'normal')
    Main_Screen.delete('1.0', END)
    searched_for = search.get()
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Name = (?)''',(searched_for,))
    list1 = list(cursor.fetchall())
    print(list1)
    cursor.execute('''SELECT COUNT(*) FROM dates WHERE Name = (?) ''',(searched_for,))
    result = cursor.fetchone()[0]
    print(result)
    for tuple_vars in list1:
        for var0, var1, var2 ,var3 in tuple_vars:
            header = '{}\n'.format(var0)
            Main_Screen.insert(tkinter.END, header)  # ADD SOME FORMATTING
            output = '{}\n{}\n{}\n{}\n'.format(var1, var2, var3)
            Main_Screen.insert(tkinter.END, output)

for i in range(0, result - 1):
    Main_Screen.insert(tkinter.END, "\n")

and get new error:

  File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 122, in searched
for var0, var1, var2 ,var3 in tuple_vars:
ValueError: too many values to unpack (expected 4)

1 Answers1

3

Never used tkinter, but thinking that you could format the entire string before sending it to tkinter:

for tuple_vars in list1:
    output = '{}\n{}\n{}\n{}\n'.format(*tuple_vars)
    Main_Screen.insert(tkinter.END, output)

Should work if its true that

list1 = [('2017-07-14', 'test', '11:00:00', '13:00:00'), 
         ('2017-07-21', 'test', '11:00:00', '13:00:00')]

To do it in two steps

for var1, var2, var3, var4 in list1:
    header = '{}\n'.format(var1)
    Main_Screen.insert(tkinter.END, header)  # ADD SOME FORMATTING
    output = '{}\n{}\n{}\n'.format(var2, var3, var4)
    Main_Screen.insert(tkinter.END, output)
Roelant
  • 4,508
  • 1
  • 32
  • 62
  • Thanks, it works :) I know this might be hard to do, but I wanted to have the date bigger than the rest of the others, like a heading. Do you know how to select it first with your method, or not. – pythonsnake Mar 10 '17 at 12:27
  • haha, no clue how you do bold in tkinter (maybe see http://stackoverflow.com/questions/40237671/python-tkinter-single-label-with-bold-and-normal-text ), but splitting should be easy. See my edit. – Roelant Mar 10 '17 at 13:15
  • Thanks for the reply @Roelant . I am a bit confused in how to combine your two aswers. The first one worked for each one in the list, and your second one, does one tuple. How would I combine them do each one in the main tuple? update the question to show newer code and new error. – pythonsnake Mar 10 '17 at 13:54
  • Changed my code (made a small mistake). In your edit code, you should not have the two loops but only the last one from my post. – Roelant Mar 10 '17 at 14:27