lundi 25 avril 2016

In Python3 using a listbox selection to insert into an sqlite3 database

I'm making a multi-window python3/tkinter/sqlite3 CRUD application. There is a database of exercises, and a database to act as the workout diary.

On the main window it shows the navigation buttons and today's diary. It works, I've tested it.

You can browse the exercises and select one. I want the selected exercise to pass to a function that adds it to the database. That function is in another file. The function works when I pass generic strings into it, but not when I try to use the listbox.

The main window opens this window when you click on a button:

def addExerciseWindow():
    newdb= db()
    toplevel = Toplevel()
    toplevel.title('Fitness Tracker')
    toplevel.focus_set()
    exercises = newdb.showAllExercises()

    ########RIGHT SIDE#########
    headerRight = Label(toplevel, text="Exercise Database", font ("Helvetica", 12))
    headerRight.grid(row =0, column=1)

    listbox = Listbox(toplevel, selectmode=SINGLE)
    listbox.grid(row=1,column=1)


    for exercise in exercises:
        listbox.insert(END, exercise)


    listbox.activate(0)

    ####LEFT SIDE########
    def getSelected():
        item = listbox.get(listbox.curselection())
        return item

My Error reads: _tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number

The database function:

def insertDiary(self, items):
    today = dt.date.today().strftime("%m-%d-%y")
    self.cursor.execute("INSERT INTO Diary (TodayDate, Exercises) VALUES(?, ?)",(today, items))
    self.conn.commit()

Aucun commentaire:

Enregistrer un commentaire