简体   繁体   中英

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.

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?

ex: (original table show)
1

2


I want to show this on the table when I add new data"3" while I click the buttom "add"

1

2

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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