简体   繁体   English

如何在不重新运行程序的情况下在 tkinter 表上显示新添加的数据?

[英]How to display new add data on tkinter table without rerun the program?

I want to show the sqlite databese on tkinter table,when I add a new data,I need to run the program again and then the new one display on the table.我想在 tkinter 表上显示 sqlite 数据库,当我添加新数据时,我需要再次运行程序,然后新的数据会显示在表上。

How can I edit the code to show the new data with old data together without run the data again?如何编辑代码以将新数据与旧数据一起显示而无需再次运行数据? I know can use the .after() or .update() but I don't know where to add?我知道可以使用 .after( .after().update()但我不知道在哪里添加?

ex: (original table show)例如:(原始表格显示)
1 1

2 2


I want to show this on the table when I add new data"3" while I click the buttom "add"当我单击按钮“添加”时添加新数据“3”时,我想在表格上显示它

1 1

2 2

3 3

Here is the whole code, thanks for help!这是整个代码,谢谢帮助!

from tkinter import *
import tkinter as tk
import sqlite3


def add():
    b1 = Button(window,text = "Add",bg="lightblue",fg = "black",command=clicked)
    b1.grid(column=0,row=0)
    window.mainloop()

   
def clicked():
    def new():
        import sqlite3 #create new customer
        conn = sqlite3.connect('d:\\hw4.db')
        cursor = conn.cursor()
        name1=e_name.get()
        gender1=e_ged.get()
        birth1=e_bir.get()
        sql="insert into ss (name,gender,birth) values ('"+name1+"','"+gender1+"','"+birth1+"')"
        cursor.execute(sql)
        conn.commit()
        conn.close()
        top.destroy()
    top = Toplevel()
    top.title("New")
    #1
    name=Label(top,text="Name: ").grid(column=3,row=0,sticky='w')
    e_name=StringVar()
    e_name=Entry(top,text=e_name)
    e_name.grid(column=4,row=0)
    #2
    name=Label(top,text="Gender: ").grid(column=3,row=1,sticky='w')
    e_ged=StringVar()
    e_ged=Entry(top,text=e_ged)
    e_ged.grid(column=4,row=1)
    #3
    name=Label(top,text="Birth: ").grid(column=3,row=2,sticky='w')
    e_bir=StringVar()
    e_bir=Entry(top,text=e_bir)
    e_bir.grid(column=4,row=2)
    
    #add
    c=Button(top,text="Add",bg='lemonchiffon',fg='black',command=new)
    c.grid(column=3,row=4,sticky='w')
    top.mainloop()

    
    
def main():
    frame=Frame(window)
    import sqlite3
    conn = sqlite3.connect('d:\\hw4.db')
    cursor = conn.cursor()
    sql = 'select rowid,* from ss'
    cursor.execute(sql)
    result=cursor.fetchall();
    for r in range (len(result)):
        rlbls=[None,None,None,None,None,None]
        rlbls[0]=Label(frame,text=str(result[r][0]))
        rlbls[0].grid(column=0,row=r)
        rlbls[1]=Label(frame,text=str(result[r][1]))
        rlbls[1].grid(column=1,row=r)
        rlbls[2]=Label(frame,text=str(result[r][2]))
        rlbls[2].grid(column=2,row=r)
        rlbls[3]=Label(frame,text=str(result[r][3]))
        rlbls[3].grid(column=3,row=r)
        btn1 = Button(window, text="edit")
        btn1.grid(column=4, row=r+1)
        btn2 = Button(window, text="delete")
        btn2.grid(column=5, row=r+1)
        
    #show    
    conn=sqlite3.connect('d:\\hw4.db')
    c = conn.cursor()
    r = conn.execute('''SELECT * from ss''');
    i =1
    for n in r:
        for j in range(len(n)):
            e=Label(window,text=n[j])
            e.grid(column=j,row=i)
        i=i+1
    
       
    conn.commit()
    conn.close()
    return frame

window = Tk()
window.title("add")
window.geometry('200x200')

main()
add()

So I made a simple sample code:所以我做了一个简单的示例代码:

from tkinter import Tk, Label, Entry, Button


data = ['Info about 1', 'Info about 2']


def submit():
    given_data = user_input.get()
    user_input.delete(0, 'end')
    data.append(given_data)
    Label(root, text=given_data).pack()


root = Tk()

user_input = Entry(root)
user_input.pack()

submit_btn = Button(root, text='Submit', command=submit)
submit_btn.pack()

for value in data:
    Label(root, text=value).pack()

root.mainloop()

So basically the important part of this is that You would have Your database and in the submit function You would just append to Your database the given info, but You would also create a new Label with that info and pack it immediately.所以基本上这个重要的部分是你将拥有你的数据库并在提交 function 你只需 append 到你的数据库给定的信息,但你也将创建一个新的 ZB021DF6AAC4654C454F46C77646 立即用该信息打包它。 Now there is the for loop so basically in Your case (You will have to adjust it for Yourself) You will still always read all the data in database when launching the program.现在有 for 循环,所以基本上在您的情况下(您必须自己调整它)在启动程序时您仍然会始终读取数据库中的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM