我有一个问题,问题是我想创建一个 post-it tkinter gui 应用程序,我想存储用户创建的所有帖子,以便他们在重新运行应用程序时可以打开它,所以我使用了 sqlite 3 模块来实现这一点,但是当用户打开现有的 post-its bcs 时,我卡住了它打开了 for 循环的最后一个内容
如果你没有得到它,这里是代码:
"""
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()
"""
如果你知道答案请帮忙
首先,不要Tk
在一个程序中多次使用- 它可能会在以后导致问题。对于所有其他窗口,请使用Toplevel
. 替换post = Tk()
为post = Toplevel()
。此处解释了
您的循环不起作用的原因。要修复它,请将您的 lambda 函数更改为lambda postit = postit: openPost(postit[2]))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句