Entry : StringVar pas mis à jour

Fermé
Pascal - 20 mars 2018 à 00:30
 Pascal - 20 mars 2018 à 23:35
Bonjour,

Le StringVar Pwd n'est jamais mis à jour avec le widget Entry dans la fonction OpenPasswordWidget(). Je ne comprends pas, comment corriger ce problème ?



Pwd = tk.StringVar()
PwdRetry = tk.IntVar()
PwdRetry.set(3)

def Verification():
if Pwd.get() == PASSWORD:
# Password correct : inhibit Pwd Button and destroy window
SetPwdButton.config(state=tk.DISABLED)
SetIpButton.config(state=tk.NORMAL)
greenStatus()
windowPwd.destroy()
else:
# Incorrect password
PwdRetry.set(PwdRetry.get() - 1)
if PwdRetry.get() == 0:
tkmessbox.showerror('Result','Maximum attempt reached\nExit !')
windowPwd.destroy()
exit(0)
Pwd.set('')
tkmessbox.showwarning('Result','Incorrect password.\nTry again !')
#os.system(command)
return

def OpenPasswordWidget():
windowPwd.title('Identification required')
editPwd = tk.Entry(windowPwd, textvariable = Pwd, show='*', bg ='bisque', fg='maroon',
exportselection=0)

BoutonOk = tk.Button(windowPwd, text ='OK', command = Verification)
editPwd.grid(row=0, column=2, padx=2, pady=2)
BoutonOk.grid(row=0, column=3, padx=2, pady=2)
editPwd.focus_set()
return

#Define a frame with 2 button, command line, result
upFrame = tk.Frame(root, width=200, height = 600)
upFrame.grid(row=0, column=0, padx=0, pady=2)

btnFrame = tk.Frame(upFrame, width=200, height = 200)
btnFrame.grid(row=1, column=0, padx=10, pady=2)

SetPwdButton = tk.Button(btnFrame, text = "Set Pwd", command = OpenPasswordWidget)
SetPwdButton.grid(row=0, column=0, padx=10, pady=2)

SetIpButton = tk.Button(btnFrame, text="Set IP")
SetIpButton.grid(row=0, column=1, padx=10, pady=2)
SetIpButton.config(state=tk.DISABLED)

statusCanvas = tk.Canvas(btnFrame, width=40, height=40, bg='white')
statusCanvas.grid(row=0, column=2, padx=10, pady=2)
orangeStatus()

root.mainloop()

exit(0)

1 réponse

Sans autres précisions, sans un code correctement indenté avec la balise code du forum, et sans code qui pourrait être testé sans modfications, c'est tout de même difficile de te venir en aide.

Toujours est-il qu'en faisant un code réduit au plus simple, cela fonctionne très bien.


import tkinter as tk

root = tk.Tk()
Pwd = tk.StringVar()

def Verification(win):
    if Pwd.get().strip():
        print(Pwd.get())
        win.destroy()
        Pwd.set('')

def OpenPasswordWidget():
    windowPwd = tk.Toplevel(root)
    windowPwd.title('Identification required')
    editPwd = tk.Entry(windowPwd, textvariable = Pwd, show='*', bg ='bisque', fg='maroon',
    exportselection=0)

    BoutonOk = tk.Button(windowPwd, text ='OK', command = lambda : Verification(windowPwd))
    editPwd.grid(row=0, column=2, padx=2, pady=2)
    BoutonOk.grid(row=0, column=3, padx=2, pady=2)
    editPwd.focus_set()

#Define a frame with 2 button, command line, result
upFrame = tk.Frame(root, width=200, height = 600)
upFrame.grid(row=0, column=0, padx=0, pady=2)

btnFrame = tk.Frame(upFrame, width=200, height = 200)
btnFrame.grid(row=1, column=0, padx=10, pady=2)

SetPwdButton = tk.Button(btnFrame, text = "Set Pwd", command = OpenPasswordWidget)
SetPwdButton.grid(row=0, column=0, padx=10, pady=2)

SetIpButton = tk.Button(btnFrame, text="Set IP")
SetIpButton.grid(row=0, column=1, padx=10, pady=2)
SetIpButton.config(state=tk.DISABLED)

statusCanvas = tk.Canvas(btnFrame, width=40, height=40, bg='white')
statusCanvas.grid(row=0, column=2, padx=10, pady=2)
#orangeStatus()

root.mainloop()


Le problème doit se situer ailleurs que dans ce que tu montres.
0
Merci pour ton aide. Concernant la mise en forme, je ferai mieux la prochaine fois.
Pour le code, il est quasiment complet. Je commence tout juste le codage de mon application.
Le problème dans mon code d'origine c'est que le stringvar pwd n'est pas remis à jour dans le widget Entry ; la comparaison avec le mot de passe est toujours fausse.
Je vais essayer les petites modifs que tu as apportées ...
Pascal
0
Je reposte mon code indenté et entre quote Python. Il ne fonctionne toujours pas. Le mot de passe n'est jamais mis à jour dans Pwd. Et quand j'affiche Pwd, il ne contient rien !
import tkinter as tk
import tkinter.messagebox as tkmessbox
import os
from itertools import count


root = tk.Tk() #Makes the window
root.wm_title('Communication tool') 
root.config(background = "#FFFFFF")

#Left Frame and its contents
leftFrame = tk.Frame(root, width=200, height = 600)
leftFrame.grid(row=1, column=0, padx=10, pady=2)

tk.Label(leftFrame, text="Instructions:").grid(row=0, column=0, padx=10, pady=2)

Instruct = tk.Label(leftFrame, text="1\n2\n2\n3\n4\n5\n6\n7\n8\n9\n")
Instruct.grid(row=1, column=0, padx=10, pady=2)

def redStatus():
    statusCanvas.create_oval(10, 10, 30, 30, width=0, fill='red')

def greenStatus():
    statusCanvas.create_oval(10, 10, 30, 30, width=0, fill='green')

def orangeStatus():
    statusCanvas.create_oval(10, 10, 30, 30, width=0, fill='orange')

windowPwd = tk.Tk()
windowPwd.grab_set()

PASSWORD = 'password'
Pwd = tk.StringVar()
PwdRetry = tk.IntVar()
PwdRetry.set(3)

def Verification(win):
    print(Pwd.get())
    if Pwd.get().strip() == PASSWORD:
        # Password correct : inhibit Pwd Button and destroy window
        SetPwdButton.config(state=tk.DISABLED)
        SetIpButton.config(state=tk.NORMAL)
        greenStatus()
        win.destroy()
    else:
        # Incorrect password
        PwdRetry.set(PwdRetry.get() - 1)
        if PwdRetry.get() == 0:
            tkmessbox.showerror('Result','Maximum attempt reached\nExit !')
            win.destroy()
            exit(0)
        Pwd.set('')
        tkmessbox.showwarning('Result','Incorrect password.\nTry again !')
        #os.system(command)
    return

def OpenPasswordWidget():
    windowPwd.title('Identification required')
    editPwd = tk.Entry(windowPwd, textvariable = Pwd, show='*', bg ='bisque', fg='maroon', 
                     exportselection=0)
    
    BoutonOk = tk.Button(windowPwd, text ='OK', command = lambda : Verification(windowPwd))
    editPwd.grid(row=0, column=2, padx=2, pady=2)
    BoutonOk.grid(row=0, column=3, padx=2, pady=2)
    editPwd.focus_set()
    return

#Define a frame with 2 button, command line, result
upFrame = tk.Frame(root, width=200, height = 600)
upFrame.grid(row=0, column=0, padx=0, pady=2)

btnFrame = tk.Frame(upFrame, width=200, height = 200)
btnFrame.grid(row=1, column=0, padx=10, pady=2)

SetPwdButton = tk.Button(btnFrame, text = "Set Pwd", command = OpenPasswordWidget)
SetPwdButton.grid(row=0, column=0, padx=10, pady=2)

SetIpButton = tk.Button(btnFrame, text="Set IP")
SetIpButton.grid(row=0, column=1, padx=10, pady=2)
SetIpButton.config(state=tk.DISABLED)

statusCanvas = tk.Canvas(btnFrame, width=40, height=40, bg='white')
statusCanvas.grid(row=0, column=2, padx=10, pady=2)
orangeStatus()

root.mainloop()

exit(0)
0
Je suis reparti de ton code en ajoutant une partie du mien, ça marche. Donc effectivement, une ligne de mon programme doit être foireuse.
Merci encore,
Pascal
0
Il faut éviter d'utiliser une seconde instance de Tk, dans 99.99999% des cas, un toplevel est suffisant.

Quelques corrections et modifications de ce que ça pourrait donner.

import tkinter as tk
import tkinter.messagebox as tkmessbox
import os
from itertools import count

PASSWORD = 'password'
    
def statusSetColor(color):
    statusCanvas.itemconfig('status', fill=color)

def Verification(win):
    print(Pwd.get())
    if Pwd.get().strip() == PASSWORD:
        # Password correct : inhibit Pwd Button and destroy window
        SetPwdButton.config(state=tk.DISABLED)
        SetIpButton.config(state=tk.NORMAL)
        statusSetColor('green')
        win.destroy()
    else:
        # Incorrect password
        PwdRetry.set(PwdRetry.get() - 1)
        if PwdRetry.get() == 0:
            tkmessbox.showerror('Result','Maximum attempt reached\nExit !', parent=win)
            win.destroy()
            SetPwdButton['state'] = tk.DISABLED
            statusSetColor('red')
            return
        Pwd.set('')
        tkmessbox.showwarning('Result','Incorrect password.\nTry again !', parent=win)
        #os.system(command)
    return

def OpenPasswordWidget():
    windowPwd = tk.Toplevel(root)
    windowPwd.transient(root)
    windowPwd.grab_set()
    windowPwd.title('Identification required')
    editPwd = tk.Entry(windowPwd, textvariable = Pwd, show='*', bg ='bisque', fg='maroon', 
                     exportselection=0)
    
    BoutonOk = tk.Button(windowPwd, text ='OK', command = lambda : Verification(windowPwd))
    editPwd.grid(row=0, column=2, padx=2, pady=2)
    BoutonOk.grid(row=0, column=3, padx=2, pady=2)
    editPwd.focus_set()
    
root = tk.Tk() #Makes the window
root.title('Communication tool') 
root.config(background = "#FFFFFF")

Pwd = tk.StringVar()
PwdRetry = tk.IntVar()
PwdRetry.set(3)

#Left Frame and its contents
leftFrame = tk.Frame(root, width=200, height = 600)
leftFrame.grid(row=1, column=0, padx=10, pady=2)

tk.Label(leftFrame, text="Instructions:").grid(row=0, column=0, padx=10, pady=2)

Instruct = tk.Label(leftFrame, text="1\n2\n2\n3\n4\n5\n6\n7\n8\n9\n")
Instruct.grid(row=1, column=0, padx=10, pady=2)

#Define a frame with 2 button, command line, result
upFrame = tk.Frame(root, width=200, height = 600)
upFrame.grid(row=0, column=0, padx=0, pady=2)

btnFrame = tk.Frame(upFrame, width=200, height = 200)
btnFrame.grid(row=1, column=0, padx=10, pady=2)

SetPwdButton = tk.Button(btnFrame, text = "Set Pwd", command = OpenPasswordWidget)
SetPwdButton.grid(row=0, column=0, padx=10, pady=2)

SetIpButton = tk.Button(btnFrame, text="Set IP")
SetIpButton.grid(row=0, column=1, padx=10, pady=2)
SetIpButton.config(state=tk.DISABLED)

statusCanvas = tk.Canvas(btnFrame, width=40, height=40, bg='white')
statusCanvas.grid(row=0, column=2, padx=10, pady=2)

statusCanvas.create_oval(10, 10, 30, 30, width=0, tag='status')
statusSetColor('orange')

root.mainloop()


Ca pourrait être plus propre en utilsant de l'objet, mais déjà il n'y a pas de global dans ton code ce qui est déjà bien.
0
OK, problème résolu. Merci.
0