Python Forum
tkinter -- after() method and return from function -- (python 3)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter -- after() method and return from function -- (python 3)
#1
Hello.I have 2 problems with my GUI. First of all I have 2 files ,one named GUI_password.py and the other one GUI_main.py.
Code of GUI_password.py below:

from tkinter import *
from tkinter import *
import tkinter.font as tkFont
from PIL import Image , ImageTk
from threading import Timer
import time
import threading
import asyncio



def delay(seconds):
      t0 = time.time()
      i = 1
      while (i <= 1):
         t1 = time.time()
         if(t1 - t0 > seconds):
            
            t0 = time.time()
            i+=1      



def Password(root , unlock_message , access_camera): # passcode => '12345'    

    activate_message = "SYSTEM ACTIVATED"
    denied_message = "ACCESS DENIED"
    wrong_attempts = 0  

    #root.configure(background = 'cyan')    # background color
    size_letter = tkFont.Font(family = "Helvetica" , size = 19 , weight = "bold")
    message_box = Label(root , text = "PLEASE ENTER ACCESS CODE" , font = size_letter).place(x = 558 , y = 30)
    
    user_input = StringVar() # user_+input holds a string (default value = "")
    input_box = Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) )
    input_box.place(x = 700 , y = 80)
    
    lock_logo = Image.open("lock_icon.jpg")
    lock_logo = lock_logo.resize( (50 , 50) , resample = 0)
    lock_image = ImageTk.PhotoImage(lock_logo)
    

    frame_for_lock_icon = LabelFrame(root , padx = 5 , pady = 5,relief = SUNKEN , bd = 0)
    label_for_lock = Label(frame_for_lock_icon , image = lock_image)
    label_for_lock.image = lock_image
    label_for_lock.grid(row = 0 , column = 0)
    frame_for_lock_icon.place(x = 725 , y = 200)
    my_var = False
  

    def check_passcode():
        nonlocal wrong_attempts , my_var     
        
        size_message_attempts = tkFont.Font(size = 13)
        
        if(input_box.get() == "12345"):
            print("RIGHT")
            frame_for_lock_icon = LabelFrame(root , padx = 5 , pady = 5,relief = SUNKEN , bd = 0)
            label_for_lock = Label(frame_for_lock_icon , image = lock_image)
            
            frame_for_lock_icon.configure(background = "green")
            frame_for_lock_icon.place(x = 725 , y = 200)
            label_for_lock.image = lock_image
            label_for_lock.grid(row = 0 , column = 0)
            entry_box = Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) , state = DISABLED).place(x = 700 , y = 80)
            button_ok = Button(root , text = "OK" , width = 7 , pady = 8 , command = check_passcode , state = DISABLED).place(x = 655 , y = 140)  
            button_cancel = Button(root , text = "CANCEL" , width = 7 , pady = 8 , command = root.quit , state = DISABLED).place(x = 770 , y = 140)
            #root.after(3000 , None)
            #frame_for_lock_icon.after_cancel(root)
            #entry_box.destroy()
            #button_ok.destroy()
            #button_cancel.destroy()
            my_var = True

        elif(input_box.get() != ""):
            blink_icon()
            Label(root , text = "(" + str(5 - wrong_attempts) + ")" , font = size_message_attempts).place(x = 585 , y = 145)
            wrong_attempts += 1
            print("WRONG")
            input_box.delete(0 , END)

        
        if(wrong_attempts == 6):
            print("6 wrong attempts were given")
            Entry(root , show = "*" , width = 6 , font = ('Verdana' , 20) , state = DISABLED).place(x = 700 , y = 80)
            Button(root , text = "OK" , width = 7 , pady = 8 , command = check_passcode , state = DISABLED).place(x = 655 , y = 140)  
            Button(root , text = "CANCEL" , width = 7 , pady = 8 , command = root.quit , state = DISABLED).place(x = 770 , y = 140)
            exit()
    
    if(my_var):
        return True
    ok_button = Button(root , text = "OK" , width = 7 , pady = 8 , command = check_passcode , borderwidth = 3).place(x = 655 , y = 140)  
    cancel_button = Button(root , text = "CANCEL" , width = 7 , pady = 8 , command = root.quit , borderwidth = 3).place(x = 770 , y = 140)  
    


    var = FALSE
    a = 1
    def blink_icon():
        #do stuff
        nonlocal var , a
        frame_for_lock_icon = LabelFrame(root , padx = 5 , pady = 5,relief = SUNKEN , bd = 0)
        label_for_lock = Label(frame_for_lock_icon , image = lock_image)
        
        t = Timer( 0.5 , blink_icon )
        t.start()
        #print("Var =   " + str(var))
        if(a == 5):
            t.cancel()
            a = 1
            return
        else:
            if(var == True):
                frame_for_lock_icon.place(x = 725 , y = 200)
                label_for_lock.image = lock_image
                label_for_lock.grid(row = 0 , column = 0)
            else:
                frame_for_lock_icon.configure(background = "red")
                frame_for_lock_icon.place(x = 725 , y = 200)
                label_for_lock.image = lock_image
                label_for_lock.grid(row = 0 , column = 0)
               
            var = not var
        a+=1
Code of GUI_main.py below:

from tkinter import *
from GUI_password import Password

root = Tk()
root.geometry("1800x1100")
root.title("Vehicle app")
access = Password(root , "unlock" , True)
if(access == True):
    print("DONE PASSWORD METHOD")

root.mainloop()
First of all, (if you run my code) ,you will understand what I want to say.
In GUI_password.py file ,in check_passcode() function ,when the passcode is correct,I want the frame_for_lock_icon to be green BUT FOR ONLY 2 SECONDS(for examle) and then I want to exit from check_passcode and from Password(...) method ,but I can't do that.
How can I fix it?

Any help will be appreciated.
Thanks.
Reply


Messages In This Thread
tkinter -- after() method and return from function -- (python 3) - by Nick_tkinter - Feb-18-2021, 08:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,088 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 867 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  Tkinter won't run my simple function AthertonH 6 3,908 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,897 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,569 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,043 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  return a variable from a function snakes 3 2,821 Apr-09-2021, 06:47 PM
Last Post: snakes
  tkinter get function finndude 2 2,978 Mar-02-2021, 03:53 PM
Last Post: finndude
  function in new window (tkinter) Dale22 7 5,196 Nov-24-2020, 11:28 PM
Last Post: Dale22
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,611 Oct-23-2020, 05:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020