The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Time.sleep() causes my GUI to "not responding"
Discuss Time.sleep() causes my GUI to "not responding" in the Python Programming forum on Dev Shed. Time.sleep() causes my GUI to "not responding" Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 9th, 2013, 06:10 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 10
Time spent in forums: 12 h 58 m 39 sec
Reputation Power: 0
|
|
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!
|

January 9th, 2013, 10:32 AM
|
 |
Contributing User
|
|
|
|
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!
|

January 9th, 2013, 10:48 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 10
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|