-2

I have a problem, the problem is i want to create a post-it tkinter gui app and I want to store all the posts the user create so they can open it when they rerun the app, so i used sqlite 3 module to achieve this, but im stuck at the moment when the user opens the existing post-its bcs it opens the last content of the for loop

In case u dont get it here is the code:

"""

from tkinter import *
import sqlite3

conn = sqlite3.connect("post-it.db")
row = 1

cursor = conn.cursor()

posts = cursor.execute("SELECT rowid, * FROM postits")

postsFetch = posts.fetchall()

print(f"{postsFetch}")

def createPost():
    pass

def openPost(name):
    print(name)

    post = Tk()
    text = Label(post,text=name)
    text.pack()
    post.mainloop()

window = Tk()
window.geometry("400x400")
window.config(bg="blue")

createNew = Button(text="Create new Post-it",command=createPost)
createNew.grid(column=1,row=1)

createName = Entry()
createName.grid(column=1,row=2)

frame = Frame()
frame.grid(column=2)

#the problem is at this for loop it opens the last item text

for postit in postsFetch:
    postitBtn = Button(frame,text=postit[1],command=lambda: openPost(postit[2]))
    postitBtn.grid(column=8,row=row)
    row += 1

conn.commit()

window.mainloop()
conn.close()

"""

if u know the answer please help

Fovu
  • 71
  • 6
  • Welcome to Stackoverflow. Before post questions you should read this [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Module_art Sep 19 '21 at 16:07
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – Delrius Euphoria Sep 19 '21 at 17:55

1 Answers1

0

Firstly, don't use Tk more than once in a program - it can cause problems later on. For all other windows, use Toplevel. Replace post = Tk() with post = Toplevel().
The reason your loop doesn't work is explained here. To fix it, change your lambda function to lambda postit = postit: openPost(postit[2]))

Henry
  • 3,472
  • 2
  • 12
  • 36