Afficher un resultat print dans un label tkinter python3

Bonjours a tous,
je souhait afficher le résultat dans un Entry tkinter le script fonctionne bien en mode console.
je demande de l’aide je suis sous v.s.code et python 3.11.1 dernière version.
cordialement, et bonne fête
kyrob

Bonjour @kyrob :slightly_smiling_face:

Voici un simple exemple, que vous pouvez essayer, il affiche le résultat de la somme de deux nombres:

from tkinter import *
import tkinter.messagebox

def show_result():
    res = int(num1.get()) + int(num2.get())
    tkinter.messagebox.showinfo('result',res)

main = Tk()

Label(main, text="Entrer le nombre 1:").grid(row=0)
Label(main, text="Entrer le nombre 2:").grid(row=1)

num1 = Entry(main)
num2 = Entry(main)

num1.grid(row=0,column=1)
num2.grid(row=1,column=1)

Button(main,text='Quitter',command=main.destroy).grid(row=4,column=0,sticky=W,pady=4)
Button(main,text='Afficher',command=show_result).grid(row=4,column=1,sticky=W,pady=4)

mainloop()

Bon courage et bonne fête :tada:

Si vous voulez afficher le résultat dans un Entry tkinter, essayer le code suivant:

from tkinter import *
from tkinter.messagebox import *

def show_result():
    somme = int(num1.get()) + int(num2.get())
    res.insert(0, somme)


main = Tk()
Label(main, text = "Entrer le nombre 1: ").grid(row=0)
Label(main, text = "Entrer le nombre 2: ").grid(row=1)
Label(main, text = "La somme est :").grid(row=2)


num1 = Entry(main)
num2 = Entry(main)
res = Entry(main) #afficher le résultat dans un Entry tkinter


num1.grid(row=0, column=1)
num2.grid(row=1, column=1)
res.grid(row=2, column=1)


Button(main, text='Quitter', command=main.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(main, text='Afficher', command=show_result).grid(row=4, column=1, sticky=W, pady=4)

mainloop()

bonjour Jacque et merci,
voici le tableau :

et le script :

   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
            print(row[0])
            
            self.information()
            messagebox.showinfo("Succes", "Enregistrement afficher")
            
    # information affichage
    def information(self):
        #cursors_row = self.row()
        #contents = self.row = (cursors_row)
        #row =contents["values"]
        self.id.get(row[0]),                 # id
        self.titre.set(row[1]),              # Titre
        self.nom.set(row[2]),                # Nom 
        self.prenom.set(row[3]),             # Prenom
        self.mail.set(row[4]),               # mail
        self.telephone.set(row[5]),          # Telephone
        self.date.set(row[6]),               # Date
        self.cotisation.set(row[7]),         # cotisation
        self.ville.set(row[8]),              # ville
        self.code_postal.set(row[9]),        # code_postal
        self.adresse.delete("1.0", END)      # Adresse
        self.adresse.insert(END, row[10]),   # Adresse          
            

l’erreur commence a partir de def information(self):
j’ai commençais le python en début d’année, je n’ai pas tous compris, mais j’avance tout doucement.
merci d’avance et bonne fête de Noël :partying_face:
cordialement Jacques
kyrob

Oui, python c’est assez facile par rapport à d’autres langages, il y’a plein de tutoriels sur le net que vous pouvez suivre pour avancer rapidement.

tu peux me dire svp quel erreur affiche sur cette ligne ? c’est peut être une erreur d’indentation(espace au début).

merci jacques.

l’erreur et la suivante :

1
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "d:\Python\projet_python\Formulaire_adherent_2\Formulaire-A.py", line 154, in afficherRechertat
    self.information()
  File "d:\Python\projet_python\Formulaire_adherent_2\Formulaire-A.py", line 162, in information
    self.id.get(row[0]),                 # id
    ^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'

cordialement
kyrob

Dans la fonction def information(self): changer la ligne suivante:

self.id.get(row[0]),

par

self.id.set(row[0]),

Je pense que çà qui cause l’erreur.

kyrob
c’est ce que je viens de faire avant de faire la copie d’écran :

self.id.set(row[0]),

et j’ai essaye remplacer set par get qui ne marche pas mieux, voici une partie du script du debut :

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)
        self.id = Entry(frame1, font=("times new roman", 15),bg ="lightgrey")
        self.id.place(x=50,y=70,width=80)
        # self.id.focus_set()
        
        # titre 
        aff_titre =Label(frame1, text="Titre", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=40)
        self.titre = Entry(frame1, font=("times new roman", 15),bg ="lightgrey")
        self.titre.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)
        self.nom = Entry(frame1, font=("times new roman", 15),bg ="lightgrey")
        self.nom.place(x=50,y=130,width=250)
        

peut-être que j’ai oublier quelque chose.
j’ai oublier de signaler que les row est soulignée dans v. s. code dans le def information(self)
cordialement Jacques
et merci encore
kyrob

En fait, la méthode get() ne reçois aucune argument, du coup tu dois juste appeler la méthode get() sans argument comme ça:

self.id.get(),                 # id

ou

self.id,                 # id

ça passe bien, mais ne s’affiche pas au tableau

def information(self):
        #cursors_row = self.row()
        #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   

les variables gardes leurs valeurs mais ne s’affiche pas dans le tableau, et passe a la fiche suivante après messagebox.showinfo(« Succes », « Enregistrement afficher »).

cordialement jacques
kyrob

Bonjours,
je n’arrive pas a afficher la valeur des variables dans Entry , les valeurs arrivent jusqu’à self.nom , me donne bien la valeur du nom , mais ne l’affiche pas ?
voici une partie du script :

# Id 
        aff_id =Label(frame1, text="Id", font = ("times new roman",15, "bold"), bg= "cyan", fg= "black").place(x=50, y=40)
        self.id = Entry(frame1, textvariable = self.id, font= ("times new roman", 15),bg ="lightgrey")
        self.id.place(x=50,y=70,width=80)
        # self.id.focus_set()
        
        # titre 
        aff_titre =Label(frame1, text="Titre", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=40)
        self.titre = Entry(frame1, textvariable=self.titre, font=("times new roman", 15),bg ="lightgrey")
        self.titre.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)
        self.nom = Entry(frame1, textvariable= self.nom, font= ("times new roman", 15),bg ="lightgrey")
        self.nom.place(x=50,y=130,width=250)
        
       
        # prenom 
        aff_prenom =Label(frame1, text="Prénom", font=("times new roman",15, "bold"), bg="cyan", fg= "black").place(x=370, y=100)
        self.prenom = Entry(frame1, textvariable= self.prenom, font=("times new roman", 15),bg ="lightgrey")
        self.prenom.place(x=370,y=130,width=250)

quelqu’un a une suggestion a me proposer ?.

merci cordialement
kyrob

Bonjour @kyrob,

Essayez d’appeler la méthode get() sur la variable self.nom comme montré ci-dessous:

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

J’ajoute cette exemple pour s’inspirer comment on peut afficher une valeur dans un Entry Tkinter:

from tkinter import *
from tkinter.ttk import *

tk=Tk()

var=StringVar(value="default value")
Entry(tk, textvariable=var).pack()
Button(tk,text='OK').pack()

mainloop()

Résultat:

tkinter

Bonjour Michel,

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

ça ne marche pas , quand je passe le curseur sur nom de self.nom.get(), il me donne bien la valeur de la variable, comme avant pour les dix autres variables. J’ai joint une image de la fiche lecture plus haut.
Je ne comprend plus rien !!!
si vous voulez plus d’information je le fournir je travail V. S. code.
kyrob