Member Avatar for leegeorg07

Hi all, I currently have some code:

import time
import Tkinter as tk

root = tk.Tk()
pass_entered= False
def show_pw():
    pw = pw_entry.get()
    if pw == "password":
        label2['text'] = pw
        pass_entered==True
        return pass_entered
    else:
        label2['text'] = "Sorry, wrong pass"
def logoff(mins):
    lbl1.config(height=3, font=('times', 20, 'bold'))
    for k in range(int(mins), 0, -1):
        lbl1["text"] = str(k)+' Minutes remaining'
        root.update()
        time.sleep(5)
        print k
        if pass_entered == True:
            break
    lbl1["text"] = "Now logging off"
    ctypes.windll.user32.LockWorkStation()    

pw_entry = tk.Entry(root, show="*")
pw_entry.focus()
button = tk.Button(root, text='override', command=show_pw)
label2 = tk.Label(root)
label1.pack()
pw_entry.pack()
button.pack()
label2.pack()
lbl1=tk.Label()
lbl1.pack()
logoff(100)
root.mainloop()

and Im trying to stop the logoff loop once the right password is enetered, but I cant seem to stop it. Can anyone help with either a fix to my current code or a different method that works?

Thanks in advance

You have to make the variable pass_entered available to the function. The simplest way is to use global pass_entered. There were some other errors ...

import time
import Tkinter as tk
import ctypes

root = tk.Tk()
pass_entered = False

def show_pw():
    global pass_entered
    pw = pw_entry.get()
    if pw == "password":
        label2['text'] = pw
        pass_entered = True
        print pw  # test
        #return pass_entered  # pass_entered goes no-where
        return
    else:
        label2['text'] = "Sorry, wrong pass"

def logoff(mins):
    global pass_entered
    label1.config(height=3, font=('times', 12, 'bold'))
    for k in range(int(mins), 0, -1):
        label1["text"] = str(k) + ' Minutes remaining'
        root.update()
        time.sleep(5)
        print k, pass_entered  # test
        if pass_entered == True:
            break
    label1["text"] = "Now logging off"
    #ctypes.windll.user32.LockWorkStation()  # remove comment later

pw_entry = tk.Entry(root, show="*")
pw_entry.focus()
button = tk.Button(root, text='override', command=show_pw)
label1 = tk.Label(root)
label2 = tk.Label(root)

pw_entry.pack()
button.pack()
label1.pack()
label2.pack()

logoff(100)

root.mainloop()
commented: great code and a great help +3
Member Avatar for leegeorg07

thanks, that works perfectly :) I owe you vega

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.