Python Forum
add content of database to text widgte with update - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: add content of database to text widgte with update (/thread-15143.html)



add content of database to text widgte with update - atlass218 - Jan-05-2019

hi; I use python 3.6 and I try to store my informations in a sqlite3 database , and display it automaticaly in a widget text tkinter with update

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import sqlite3

master=Tk()
master.geometry('450x300')
master.title('add into database')

var_Name=StringVar()
var_City=StringVar()
contenu_database=StringVar()

CreateDataBase = sqlite3.connect('MyDataBase.db')

def CreateTable():
	QueryCurs = CreateDataBase.cursor()
	QueryCurs.execute('''CREATE TABLE IF NOT EXISTS Clients
    (id INTEGER PRIMARY KEY, Name TEXT, City TEXT)''')
	QueryCurs.close()

def AddEntry():
	city_get=str(var_City.get())
	name_get=str(var_Name.get())

	QueryCurs = CreateDataBase.cursor()
	QueryCurs.execute('''INSERT INTO Clients (Name,City) VALUES (?,?)''',(name_get,city_get))
	CreateDataBase.commit()
	var_Name.set("")
	var_City.set("")
	QueryCurs.close()

def afficher_contenu_database():

	s = Scrollbar(master)
	T= Text(master)
	s.pack(side=RIGHT, fill=Y)
	T.pack(side=LEFT, fill=Y)
	s.config(command=T.yview)
	T.config( font=('arial',14,'bold'),yscrollcommand=s.set)
	
	QueryCurs = CreateDataBase.cursor()		
	for resultats in QueryCurs.execute('SELECT * FROM Clients ORDER BY id DESC'):		
		contenu_database= str(resultats[1])+'\t'+ str(resultats[2])+"\n" 
		T.insert(END,contenu_database)	
	T.after(1000,afficher_contenu_database)
	QueryCurs.close()

label_Name=Label(master, text ='enter your name  :')
label_Name.pack()

entry_Name=Entry(master,textvariable=var_Name)
entry_Name.pack()

label_City=Label(master, text ='enter name of city  :')
label_City.pack()

entry_City=Entry(master,textvariable=var_City)
entry_City.pack()

btn_Valider=Button(master,text='validate', command=AddEntry)
btn_Valider.pack()

btn_afficher_database=Button(master,text='afficher database data', command=afficher_contenu_database)
btn_afficher_database.pack()

CreateTable()	
afficher_contenu_database()

master.mainloop()
I need help to know why the update does not work
thanks


RE: add content of database to text widgte with update - woooee - Jan-06-2019

You are creating a new Text widget on each pass through afficher_contenu_database() Create it once and delete the previous data if you don't want it.
##create Text once
s = Scrollbar(master)
T= Text(master)
s.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
s.config(command=T.yview)
T.config( font=('arial',14,'bold'),yscrollcommand=s.set)
     
def afficher_contenu_database():
    ##  call this function with the button click every time you want to update 

    ## delete previous text
    T.delete('1.0', END)
    T.update()

    QueryCurs = CreateDataBase.cursor()     
    for resultats in QueryCurs.execute('SELECT * FROM Clients ORDER BY id DESC'):       
        contenu_database= str(resultats[1])+'\t'+ str(resultats[2])+"\n" 
        T.insert(END,contenu_database)  
    ##T.after(1000,afficher_contenu_database)
    QueryCurs.close()