Bonjour,

sur un exercice demandant à introduire une date, d'en vérifier le format, ceci fonctionne.
je trébuche une difficulté pour imposer des deux événements Bind de type <Tab> et <Button-1> sur le widget Entry.
Seul l'événement <Tab> fonctionne.
Si j'introduit une date puis clique directement dans le Entry KM_Compteur, j'obtient cette erreur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
    print("addData - Date Consommation - Entry - Date_Plein", Date_Plein)
NameError: name 'Date_Plein' is not defined
ça signifie que l'événement n'a pas été sollicité.
Comment imposer l'activation de l'événement, si l'utilisateur clique sur une autre widget entre?




Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# coding:utf-8
# version 3.x python
 
 
from tkinter import *
import tkinter as tk
from tkinter.ttk import *
 
from tkinter.ttk import Button
from tkinter import font
from tkinter.scrolledtext import ScrolledText
from tkinter import messagebox                                                                          # Librairie des fenêtres d'alerte
 
from datetime import datetime                                                                            # Librairie de gestion des dates et heures
 
# =========================== Fenêtre principale  ==========================
root = tk.Tk()
root.title('Nouvelle consommation de carburant')
root.geometry("706x307+1100+700")                                                                    # ("950x355+30+30")
root.resizable(width=False, height=False)                                                         # Fenêtre verrouillée
root.attributes("-toolwindow", 1)                                                                       # Supprime les boutons Réduire/Agrandir
root.attributes("-topmost", 1)                                                                           # au priemier plan
 
# ============================== Cadres  ==============================
FrameLeft = LabelFrame(root, text="[Nouveau plein]", width=300, height=140)
FrameLeft.grid(row=0, column=0, sticky=W)
 
ConsoKM = IntVar()
# ============================== Fonctions ========================
#
def ClearEntry():
    txt_ConsoDate.delete(0, END)
    txt_ConsoDate.focus_force()
    txt_ConsoKM.delete(0, END)
 
#
def addData():
    #
    if txt_ConsoDate.get() != "":    # and txt_ConsoKM.get() != "" and txt_ConsoPlein.get() != "" and txt_ConsoPrix.get() != "":
        print("\n-----------------------------------------------------------------------------------------------")
        print("addData - Date Consommation - Entry - txt_ConsoDate.get()", txt_ConsoDate.get())
        print("addData - Date Consommation - Entry - Date_Plein", Date_Plein)
        print("addData - Date Consommation - Entry - txt_ConsoKM.get()", txt_ConsoKM.get())
        print("\n")
 
    else:
        # La fenêtre msgBox apparaît sur le fenêtre enfant SAF_SD sans l'affichage de le fenêtre master Main
        messagebox.showwarning("Attention", "Veuillez remplir tous les champs")
        root.deiconify()
 
# [Entry Widget] -
def onClick_Dtr(event, obj):
    # Curseur après la valeur de  la variable
    # print("onClick_Dtr - obj", obj)
    obj.focus()
    if obj == FrameLeft.winfo_children()[1]:
        print("**")
        saisieDateConso(event, obj)
 
# Validation de la date
def saisieDateConso(event, obj):
    print("saisieDateConso - obj -->    ", obj, obj.get(), type(obj.get()))
    if event.widget == obj:
        try:
            # -- Analyse --
            # Conversion d'une chaînes en date avec strptime
            # strptime --> analyse une chaîne de caractères dans un objet datetime en fonction du format de correspondance donné
            # %d Jour du mois sur 2 chiffres
            # %m Numéro du mois sur 2 chiffres
            # %Y     Année complète sur 4 chiffres
            datetime.strptime(obj.get(), '%d.%m.%Y')
            global Date_Plein
            Date_Plein = ""
            Date_Plein = datetime.strptime(obj.get(), '%d.%m.%Y').date()
            print("saisieDateConso - Date_Plein - datetime.strptime(obj.get(), '%d.%m.%Y')  ", Date_Plein, type(Date_Plein))
 
        except ValueError:
            messagebox.showwarning("Attention", "Veuillez saisir une date valide" + "\n" + "              jj.mm.aaaa")
            root.withdraw()
            root.deiconify()
            obj.delete(0, 'end')
 
 
# ============================== Widgets ==============================
 
#
lblConsoDate = Label(FrameLeft, text="Date")
lblConsoDate.place(x=2, y=25)
 
txt_ConsoDate = Entry(FrameLeft, text="", width=11)
txt_ConsoDate.focus_force()
txt_ConsoDate.place(x=88, y=26)
txt_ConsoDate.insert(0, "jj.mm.aaaa")
# print("Identification Entry Date Consommation - FrameLeft.winfo_children()[1]            							", FrameLeft.winfo_children()[1])
 
txt_ConsoDate.bind("<Tab>", lambda event, obj=FrameLeft.winfo_children()[1]: onClick_Dtr(event, obj))
txt_ConsoDate.bind("<Button-1>", lambda event, obj=FrameLeft.winfo_children()[1]: onClick_Dtr(event, obj))
 
#
lblConsoKM = Label(FrameLeft, text="KM_Compteur")
lblConsoKM.place(x=2, y=46)
txt_ConsoKM = Entry(FrameLeft, textvariable=ConsoKM, width=33)
txt_ConsoKM.place(x=88, y=47)
txt_ConsoKM.bind("<Tab>", lambda event, obj=txt_ConsoKM: onClick_Dtr(event, obj))
 
 
 
btnAddData = tk.Button(root, text="Ajouter", font=('verdana', 8, ''), state=NORMAL, command=addData)
btnAddData.place(x=12, y=150)
 
root.mainloop()

merci de votre temps