| 12
 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
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 
 | from tkinter import *
 
# class Saisie(object):
#     "recup des saisies"
#     def __init__(self,x=0):
#         self.x=x
#  
def affiche():
    value = 0
    for e in entries:
        s = e.get()
        if s:
            try:
                v = float(s)
            except:
                return
        else:
            v = 0
        value += v    
 
    etq1['text'] = str(value)
 
def do_validate(entry):
    print ('do_validate:', entry)
 
    ix = entries.index(entry)
    ix = (ix + 1) % len(entries)
    next_entry = entries[ix]
    print ('do_validate, next:', next_entry)
 
    if entry.get()=='':
        affiche()
        next_entry.focus_set()
        return True
    else:
        try:
            if (float(entry.get())>0) == True:
                #s1.x=float(e1Var.get())
                print('yes: ', entry, entry.get()) # pour trace 
                next_entry.focus_set() ; next_entry.selection_range(0,END)
                affiche()
                return 1
            else:
                return 0
        except:
            return 0
 
def do_invalidate(entry):
    print('do_invalidate:', entry) # pour trace
    entry.focus_set()
    entry.selection_range(0,END) # re-select saisie
    return 1    
 
def valid_e1():
    print('valid_e1') # pour trace
    return do_validate(e1)
 
#     if e1.get()=='':
#         s1.x=0 ; affiche()
#         e2.focus_set()
#         return 1
#     else:
#         try:
#             if (float(e1.get())>0) == True:
#                 s1.x=float(e1Var.get())
#                 print('yes e1',e1.get()) # pour trace 
#                 e2.focus_set() ; e2.selection_range(0,END)
#                 affiche()
#                 return 1
#             else:
#                 return 0
#         except:
#             return 0
 
def invalid_e1():
    print('invalid_e1') # pour trace
    return do_invalidate(e1)
#     e1.focus_set()
#     e1.selection_range(0,END) # re-select saisie
#     return 1    
 
def valid_e2():
    print('valid_e2') # pour trace 
    return do_validate(e2)
#     if e2.get()=='':
#         s2.x=0 ; affiche()
#         e3.focus_set()
#         return 1
#     else:
#         try:
#             if (float(e2.get())>0) == True:
#                 s2.x=float(e2Var.get())
#                 print('yes e2',e2.get()) # pour trace 
#                 e3.focus_set() ; e3.selection_range(0,END)
#                 affiche()                
#                 return 1
#             else:
#                 return 0
#         except:
#             return 0
 
def invalid_e2():
    print('invalid_e2') # pour trace
    return do_invalidate(e2)
#     
#     e2.focus_set()
#     e2.selection_range(0,END) # re-select saisie
#     return 1
#  
def valid_e3():
    print('valid_e3') # pour trace
    return do_validate(e3)
#     if e3.get()=='':
#         s3.x=0 ; affiche()
#         e1.focus_set()
#         return 1
#     else:
#         try:
#             if (float(e3.get())>0) == True:
#                 s3.x=float(e3Var.get())
#                 print('yes e3',e3.get()) # pour trace 
#                 e1.focus_set() ; e1.selection_range(0,END)
#                 affiche()                
#                 return 1
#             else:
#                 return 0
#         except:
#             return 0   
 
def invalid_e3():
    print('invalid_e3') # pour trace
    return do_invalidate(e3)
#     e3.focus_set()
#     e3.selection_range(0,END) # re-select saisie
#     return 1
 
def tabul(event):
    fen.event_generate('<Tab>')
 
def onClick(event):
    return 'break'
 
def quitter(event=None):
    fen.quit()
 
if __name__ == '__main__': 
    # fenêtre principale ***************************************
    fen = Tk()
    fen.title("Exo Entrées controlées")
    fen.geometry ("600x500+150+100")
#     ###----
#     e1Var=StringVar(); e2Var=StringVar(); e3Var=StringVar() # StringVar()
#     ###----
#     s1=Saisie() ; s2=Saisie() ; s3=Saisie() ## valeurs des saisies
#     ###----
    etq1=Label(fen, text='', width=10, bg='ivory')  #=================
    etq1.place(x=80, y=120)
    ###----
    e1=Entry(fen, width=10, textvariable=e1Var, validate='focusout',
            validatecommand=valid_e1, invalidcommand=invalid_e1, name="e1")
    e1.place(x=80, y=80) ; e1.focus_set()
    e1.bind("<Return>",tabul) # ------------------
    e1.bind("<Button-1>", onClick ) 
    ###----
    e2=Entry(fen, width=10, textvariable=e2Var, validate='focusout',
            validatecommand=valid_e2, invalidcommand=invalid_e2, name='e2')
    e2.place(x=180, y=80)
    e2.bind("<Return>",tabul) # ------------------
    e2.bind("<Button-1>", onClick )
    ###----
    e3=Entry(fen, width=10, textvariable=e3Var, validate='focusout',
            validatecommand=valid_e3, invalidcommand=invalid_e3, name='e3')
    e3.place(x=280, y=80)
    e3.bind("<Return>",tabul) # ------------------
    e3.bind("<Button-1>", onClick ) 
#++
    entries = [ e1, e2, e3 ]
#--
    ###----
    btQuitter = Button(fen, text="QUITTER", font=("Arial",-12),
        takefocus=True, bg='bisque', fg='navy')
    btQuitter.place(x=500, y=465)
    btQuitter.bind("<Return>", quitter)
    btQuitter.bind("<Button>", quitter)
    ###----
    fen.protocol("WM_DELETE_WINDOW", quitter)
    fen.mainloop()
    fen.destroy() | 
Partager