Tkinter entry textvariable

Bonjour,

self.nom = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")

ça ne marche pas , quand je passe le curseur sur nom de self.nom, il me donne bien la bonne valeur de la variable, comme avant, pour les dix autres variables aussi. Mais dans le champ Entry rien ne s’affiche. J’ai joint une image de la fiche lecture.

 # champs du formulaire
        frame1 = Frame(self.root, bg="cyan")  
        frame1.place(x=450, y=120, width=700, height=440)

        title = Label(frame1, text="Lecture fiche", font=("ink Free", 25, "bold"), bg="cyan", fg="red").place(x=140, y=20)


        # Id 
        aff_id =Label(frame1, text= "Id", font = ("times new roman",15, "bold"), bg= "cyan", fg= "black").place(x=50, y=40)
        id_txt = Entry(frame1, textvariable = self.id, font= ("times new roman", 15),bg ="lightgrey")
        id_txt.place(x=50,y=70,width=80)
        # id_txt.focus_set()
        
        # titre 
        aff_titre =Label(frame1, text= "Titre", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=40)
        titre_txt = Entry(frame1, textvariable= self.titre, font=("times new roman", 15),bg ="lightgrey")
        titre_txt.place(x=370,y=70,width=80)
        
        # nom 
        aff_nom =Label(frame1, text= "Nom", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=50, y=100)
        nom_txt = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")
        nom_txt.place(x=50,y=130,width=250)
        

Je ne comprend plus rien !!! :grimacing:
si vous voulez plus d’information je peux le fournir je travail V. S. code et Python 3.11.1.
cordialement.
:partying_face: Bonne fête de Noël.
kyrob

Bonjour @kyrob

Veuillez essayer le code suivant:

nom_var = tk.StringVar(self.nom)
nom_txt = Entry(frame1, textvariable=nom_var)
nom_txt.pack()

Et dites moi si ça marche ou il vous affiches des erreurs.

Vérifier ton code, peut-étre que tu as deux instances de TK(), l’un écrase l’autre.

bonjour,

ligne 215 = obj =Formulaire(root)

cordialement
kyrob

Ok, comment vous instanciez l’objet tk dans votre code? Normalement ça doit être:

tk=Tk()

c’est root

root=Tk()

C’est le script complet

#!/usr/bin/python               -> page-de fichier formulaire.py python
# -*- coding: utf-8 -*-         -> formulaire d'inscription Formulaire sqlite3
# Python 3.11.1                   -> jacky michaud

# import string
# import tkinter as tk
import sys,io,os
from tkinter import *
# from tkinter import messagebox
from tkinter import ttk, messagebox
from tkinter.messagebox import *
import sqlite3




class Formulaire:
    
    def __init__(self, root):
        self.root = root
        self.root.title("Formulaire du jacky")
        self.root.geometry("1500x760+15+10")  

        # les variables
        self.id = StringVar()            # id
        self.titre = StringVar()         # Titre
        self.nom = StringVar()           # Nom
        self.prenom = StringVar()        # Prenom
        self.mail = StringVar()          # Mail
        self.telephone = StringVar()     # Telephone
        self.date = StringVar()          # Date
        self.cotisation = StringVar()    # cotisation
        self.ville = StringVar()         # ville
        self.code_postal = StringVar()   # code_postal
        self.adresse = StringVar()       # adresse
        
        
        # champs du formulaire
        frame1 = Frame(self.root, bg="cyan")  
        frame1.place(x=450, y=120, width=700, height=440)

        title = Label(frame1, text="Lecture fiche", font=("ink Free", 25, "bold"), bg="cyan", fg="red").place(x=140, y=20)


        # Id 
        aff_id =Label(frame1, text= "Id", font = ("times new roman",15, "bold"), bg= "cyan", fg= "black").place(x=50, y=40)
        id_txt = Entry(frame1, textvariable = self.id, font= ("times new roman", 15),bg ="lightgrey")
        id_txt.place(x=50,y=70,width=80)
        # id_txt.focus_set()
        
        # titre 
        aff_titre =Label(frame1, text= "Titre", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=40)
        titre_txt = Entry(frame1, textvariable= self.titre, font=("times new roman", 15),bg ="lightgrey")
        titre_txt.place(x=370,y=70,width=80)
        
        # nom 
        aff_nom =Label(frame1, text= "Nom", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=50, y=100)
        nom_txt = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")
        nom_txt.place(x=50,y=130,width=250)
        #nom_var = tk.StringVar(self.nom)
        #nom_txt = Entry(frame1, textvariable=nom_var)
        #nom_txt.pack()
        
        # prenom 
        aff_prenom =Label(frame1, text= "Prénom", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=100)
        prenom_txt = Entry(frame1, textvariable= self.prenom, font=("times new roman", 15),bg ="lightgrey")
        prenom_txt.place(x=370,y=130,width=250)
        
        # mail
        aff_mail =Label(frame1, text= "E-mail", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=50, y=160)
        mail_txt = Entry(frame1, textvariable= self.mail, font= ("times new roman", 15),bg ="lightgrey")
        mail_txt.place(x=50,y=190,width=250)
        
        # telephone
        aff_telephone =Label(frame1, text= "Téléphone", font= ("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=160)
        telephone_txt = Entry(frame1, textvariable= self.telephone, font=("times new roman", 15),bg ="lightgrey")
        telephone_txt.place(x=370,y=190,width=250)
        
        # date
        aff_date =Label(frame1, text= "Date", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=50, y=220)
        date_txt = Entry(frame1, textvariable= self.date, font= ("times new roman", 15),bg ="lightgrey")
        date_txt.place(x=50,y=250,width=250)
        
        # cotisation
        aff_cotisation =Label(frame1, text= "Cotisation", font= ("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=220)
        cotisation_txt = Entry(frame1, textvariable= self.cotisation, font= ("times new roman", 15),bg ="lightgrey")
        cotisation_txt.place(x=370,y=250,width=250)
        
        # code_postal
        aff_code_postal =Label(frame1, text= "Code_postal", font= ("arial",15, "bold"), bg="cyan", fg= "black").place(x=50, y=280)
        code_postal_txt = Entry(frame1, textvariable= self.code_postal, font= ("times new roman", 15),bg ="lightgrey")
        code_postal_txt.place(x=50,y=310,width=250)
        
        # adresse
        aff_adresse =Label(frame1, text="Adresse", font= ("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=280)
        adresse_txt = Entry(frame1, textvariable= self.adresse, font= ("times new roman", 15),bg ="lightgrey")
        adresse_txt.place(x=370,y=310, width=300, height=90)
        
        # ville
        aff_ville =Label(frame1, text="Ville", font= ("arial",15, "bold"), bg= "cyan", fg= "black").place(x=50, y=340)
        ville_txt = Entry(frame1, textvariable= self.ville, font= ("times new roman", 15),bg ="lightgrey")
        ville_txt.place(x=50,y=370,width=250)
        
        
        
        
        
        # les boutons de validation et connexion
        # btn = Button(frame1, text="Creer", cursor="hand2", command=self.creer, font=("times new roman", 15, "bold"), bg="cyan", fg="black").place(x=250, y=430, width=250)
        
        # connexion
        btn1 = Button(frame1, text="Affichage", command=self.afficherRechertat, cursor="hand2", font=("times new roman", 15, "bold"), bg="cyan", fg="black").place(x=500, y=50, width=150)

############################

            
    
    def afficherRechertat(self):
               
        con = sqlite3.connect("creerinscript.db")
        cur = con.cursor()
        cur.execute("select * from formordi ")  
        rows = cur.fetchall()
        
        for row in rows:
            self.id = row[0]                # id
            self.titre = row[1]             # titre
            self.nom = row[2]               # Nom
            self.prenom = row[3]            # Prenom
            self.mail = row[4]              # mail
            self.telephone = row[5]         # telephone
            self.date = row[6]              # Date
            self.cotisation = row[7]        # cotisation
            self.ville = row[8]             # ville
            self.code_postal = row[9]       # code_postal
            self.adresse = row[10]          # Adresse
            
            
            self.information()
            
           
            messagebox.showinfo("Succes", "Enregistrement afficher")
            self.reini()
            
            con.commit()
            con.close()
        
        
    # information affichage
    def information(self):
        #cursors_row = self.tabl_resul.focus()
        #contents = self.row = (cursors_row)
        #row =contents["values"]
        self.id,                 # id
        self.titre,              # Titre
        self.nom,                # Nom 
        self.prenom,             # Prenom
        self.mail,               # mail
        self.telephone,          # Telephone
        self.date,               # Date
        self.cotisation,         # cotisation
        self.ville,              # ville
        self.code_postal,        # code_postal
        #self.adresse.delete("1.0", END)      # Adresse
        self.adresse,            # Adresse          
            

# reinitialisation
    def reini(self):
        self.id=""                  # id
        self.titre=""               # Titre
        self.nom=""                 # Nom 
        self.prenom=""              # Prenom
        self.mail=""                # mail
        self.telephone=""           # Telephone
        self.date=""                # Date
        self.cotisation=""          # cotisation
        self.ville=""               # ville
        self.code_postal=""         # code_postal
        self.adresse=""             # Adresse
    
  
  
    # def fenetre_login(self):
        # self.root.destroy()
        # import Login
        
        

        
        self.afficherRechertat()
        self.information()
        self.reini()
 
root=Tk()
obj =Formulaire(root)
root.mainloop()


Ok, je vois, essayez d’utiliser la méthode insert() du widget Entry. Dans la fonction def afficherRechertat(self): remplacez la ligne:

self.nom = row[2]

par:

nom_txt.insert(0, row[2])

et faites la même chose pour les autres.

bonjour a tous, et joyeuses fêtes :partying_face: :star_struck:

Michel, j’ai essaye l’exemple , ça ne fonctionne pas.
pour moi l’erreur viens textvariable ou de Entry par ce que j’ai pris la même base de script d’un script qui fonctionne bien.

nom_txt = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")

comme je disais plus haut, quand je passe la souris sur le nom il affiche le nom qui est dans la variable a chaque fiche que je passe (quinze noms), la souris sur self ,elle affiche la fiche entière, pour les autres variables, c’est pareille , c’est juste pour une lecture de la fiche, j’ai rajouté un module de tri qui transforme certaines minuscule en majuscule qui fonctionne en mode console.
peu-t’on remplacer par d’autres fonctions (mot clé) pour l’affichage.
si je tape une erreur comme ça :

# nom 
        aff_nom =Label(frame1, text= "Nom", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=50, y=100)
        nom_txt = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")
        nom_txt.insert(0,self.nom)
        nom_txt.place(x=50,y=130,width=250)

il me sort une erreur : « PY_VAR2 » ce qui est normale, mais il l’affiche dans la fenêtre de nom .
je vous souhaite de bonne fête de Noël.
cordialement.
kyrob