#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Question Time.sleep() causes my GUI to "not responding"


    Hello guys. My problem is that i made a loop with if statement where i made a delay with time.sleep() to prevent a program from taking too much memory while running. Anyway this is how it looks.
    Code:
     def activated1():
            cstrikefolder = cstrike.get()
            dir = os.getcwd()
            
            time.sleep(6)
            if os.path.isfile(cstrikefolder+"/censuree.cfg"):  #Checks if censuree.cfg exists
                os.remove(cstrikefolder+"/censuree.cfg")  # If it does exist then remove it ( censuree.cfg )
                shutil.copy2(dir+'/config.cfg', cstrikefolder+"/config.cfg") # And also copy the config.cfg from
                activated1()
            else:            
                activated1()
    So, when i activate the function program freezes, but still work. But it's worthless since you can't do anything to program after you activate that function. Only thing you can do is let it loop or exit the program ( end process ).

    I heard that threading can fix this, but trust me.. I really have no idea how, i tried so much to lookup on other forums but i just don't get it. Can someone help me and show me part of threading where i can fix this code to prevent it from freezing?

    Thank you in advance!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Your function recurses. When the stack overflows your program dies horribly. The gui freezes because activated1 never returns. Which library provides your graphics? gtk? tkinter? other? You haven't provided enough source code for me to see how activated1 ties in with the whole program.

    tkinter.Tk has an "after" function that can provide the delay.
    Originally Posted by tkinter online documentation
    | after(self, ms, func=None, *args)
    | Call function once after given time.
    |
    | MS specifies the time in milliseconds. FUNC gives the
    | function which shall be called. Additional parameters
    | are given as parameters to the function call. Return
    | identifier to cancel scheduling with after_cancel.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Your function recurses. When the stack overflows your program dies horribly. The gui freezes because activated1 never returns. Which library provides your graphics? gtk? tkinter? other? You haven't provided enough source code for me to see how activated1 ties in with the whole program.

    tkinter.Tk has an "after" function that can provide the delay.
    I am using tkinter. Thank you for reply, if you could show me how would that work? I would be thankful very much. Please edit my code.

    Code:
    def start():
        #Start of program
    
        pwapp.destroy()
                
        root = Tk()
        root.configure(background='black')
        root.minsize(300,150)
        root.geometry("300x150")
        root.maxsize(300, 150)
        root.wm_title("Censure worm vBETA")
        #----- Functions here--------
        
        def activated1():
            cstrikefolder = cstrike.get()
            dir = os.getcwd()
            
            time.sleep(6)        
            if os.path.isfile(cstrikefolder+"\censuree.cfg"):  #Checks if censuree.cfg exists
                os.remove(cstrikefolder+"\censuree.cfg")  # If it does exist then remove it ( censuree.cfg )
                shutil.copy2(dir+'\config.cfg', cstrikefolder+"\config.cfg") # And also copy the config.cfg from
                activated1()
            else:
                activated1()
    
    
    
                
        def activated():
            cstrikefolder = cstrike.get()                                                                     
            dir = os.getcwd()
            shutil.copy2(cstrikefolder+"\config.cfg", dir+'\config.cfg')
            activated1()
            
        
                
                
    
        def activation():
            cstrikefolder = cstrike.get()
            dir = os.getcwd()
            if os.path.isdir(cstrikefolder):
                activated()
            else:
                wrongdir =   tkMessageBox.showerror("Wrong directory", "following directory doesn't exist: "+cstrikefolder +"Example: C:\games\CS1.6v44\cstrike")
                
                
    
    
        #----- Widgets here----------
    
        title = Label(root,text="censure worm", font = "Verdana 14 bold",fg="green",bg="black")
        title.pack()
    
        title1 = Label(root,text="beta",font="verdana 8 italic",fg="red",bg="black")
        title1.place(x=230,y=15)
    
        cstrike = Entry(root)
        cstrike.place(x=20,y=50,width=150)
        cstrike.delete(0,END)
        hint1 = Label(root,text="Path to cstrike foler",bg="black",fg="white")
        hint1.place(x=20,y=30)
    
        activate_b = Button(root,bg="dark green",fg="black",text="Activate",command=activation,borderwidth=0)
        activate_b.place(x=175,y=47,height=25)
    
        stop_b = Button(root,bg="dark red",fg="black",command=start,text="stop",borderwidth=0)
        stop_b.place (x=240, y=47,height=25)
    
        copyrights = Label(root,text="Coded by Ante Gulin. Theguxi@hotmail.com",bg="black",fg="grey")
        copyrights.place(x=5,y=132)
                           
    
    
        root.mainloop()
    Thank you in advance!

    edit** Does it have to be in class? i haven't learn'd them yet so don't understand the self thing and such. So does it?

IMN logo majestic logo threadwatch logo seochat tools logo