Python Forum
add content of database to text widgte with update
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add content of database to text widgte with update
#1
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
Reply
#2
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() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,952 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Get last row of SQL database and update Turtle 5 3,122 Oct-14-2021, 07:06 PM
Last Post: Turtle
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 4,366 May-08-2021, 04:58 PM
Last Post: ibreeden
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,670 Dec-16-2020, 05:26 AM
Last Post: Vokofe
Photo Update database in tkinter shahulvk 3 3,129 Oct-24-2020, 04:48 PM
Last Post: shahulvk
  Opening file and outputting its text content example leodavinci1990 1 1,877 Oct-12-2020, 05:33 AM
Last Post: buran
  copy content of text file with three delimiter into excel sheet vinaykumar 0 2,344 Jul-12-2020, 01:27 PM
Last Post: vinaykumar
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,097 Jun-29-2020, 08:51 PM
Last Post: buran
  How to update sql database from csv Prince_Bhatia 0 2,582 Feb-09-2019, 09:15 PM
Last Post: Prince_Bhatia
  How do i read particular text from text file and update those values in MS SQL table ganeshsai2912 33 14,841 Nov-20-2018, 12:55 AM
Last Post: ganeshsai2912

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020