Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 9th, 2013, 06:10 AM
Guxi Guxi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 Guxi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 58 m 39 sec
Reputation 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!

Reply With Quote
  #2  
Old January 9th, 2013, 10:32 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
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.
Quote:
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!

Reply With Quote
  #3  
Old January 9th, 2013, 10:48 AM
Guxi Guxi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 Guxi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 58 m 39 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Time.sleep() causes my GUI to "not responding"

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap