Tkinter GUI中未正确显示的行

2024-05-18 15:48:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用TKinter建立一个GUI,创建一个基本程序,允许我添加、更新、删除和查看我个人图书馆中的书籍。我在代码的两个部分上苦苦挣扎:

  1. 当我单击ViewRecords时,GUI中的文本框应该在一个列表中显示记录,每个项都显示在自己的行中。

  2. 它也不应该在每次按下按钮时复制列表。你知道吗

    查看按钮

    view_records = Button(root, text="View Records", width=12, command=view_command)
    view_records.grid(row=3,column=3,columnspan=2,sticky='w’)
    
    def view_command():
        listings.delete(0,END) # does not duplicate entries for multiple clicks
        for row in personal_library_backend.view():
            listings.insert(END,row) # this does not list rows per index
    

这是我的后台脚本:

def insert(title, author, genre, year):
    conn=sqlite3.connect("library.db")
    cur=conn.cursor()
    cur.execute("INSERT INTO books VALUES (NULL,?,?,?,?)",(title,author,genre,year))
    conn.commit()
    conn.close()

def view():
    conn=sqlite3.connect("library.db")
    cur=conn.cursor()
    cur.execute("SELECT * FROM books")
    rows=cur.fetchall()
    conn.close()
    return rows

当我运行这个程序时,我不断得到一个错误,在view\u命令函数中显示“坏文本索引0”。我好像想不通。有人能给我指出正确的方向吗?你知道吗


Tags: 程序view列表deflibraryguiconn按钮
1条回答
网友
1楼 · 发布于 2024-05-18 15:48:00

文本小部件的索引应该是one of these格式(即)行.列“油箱端你的0不适合这些。你知道吗

应将“删除行”更改为:

listings.delete("1.0", "end") #you can use END instead of "end" 

删除文本小部件中的所有内容。你知道吗

要使每个row出现在新行上,只需在插入row之后手动插入新行。你知道吗

for row in personal_library_backend.view():
    listings.insert("end", row)
    listings.insert("end","\n")
#there is an extra new line at the end, you can remove it using 
txt.delete("end-1c", "end")

相关问题 更多 >

    热门问题