[Tkinter-discuss] Stopping a for loop with a sleep() funciton in it

Alexnb alexnbryan at gmail.com
Fri Jul 11 07:35:03 CEST 2008




Alexnb wrote:
> 
> yes I know it runs in milliseconds. So what do you suggest? 1 millisecond
> is about what I want. it was .1 seconds for the sleep() time. 
> I didn't write this code I found it online so I don't really understand
> it, but I know that is where the problem is, the for loop.
> 
> Guilherme Polo wrote:
>> 
>> On Thu, Jul 10, 2008 at 8:45 PM, Guilherme Polo <ggpolo at gmail.com> wrote:
>>> On Thu, Jul 10, 2008 at 8:24 PM, Alexnb <alexnbryan at gmail.com> wrote:
>>>>
>>>>
>>>>
>>>> Guilherme Polo wrote:
>>>>>
>>>>> On Thu, Jul 10, 2008 at 7:31 PM, Alexnb <alexnbryan at gmail.com> wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>> Guilherme Polo wrote:
>>>>>>>
>>>>>>> On Thu, Jul 10, 2008 at 5:57 PM, Alexnb <alexnbryan at gmail.com>
>>>>>>> wrote:
>>>>>>>>
>>>>>>>> Okay, so I have a for loop with a sleep command. I want the loop to
>>>>>>>> continue
>>>>>>>> until it is told to stop. I want to tell it to stop when a list
>>>>>>>> goes
>>>>>>>> from
>>>>>>>> empty to having something. The problem is that when that loop
>>>>>>>> starts,
>>>>>>>> the
>>>>>>>> program pretty much stops with it.
>>>>>>>
>>>>>>> You need to remove the use of sleep and use "after" instead. You
>>>>>>> keep
>>>>>>> scheduling your task till the condition is not met anymore, then you
>>>>>>> stop scheduling it with "after".
>>>>>>>
>>>>>>>> To make things harder, I really want that
>>>>>>>> to be it's own class, so I have to pass it the list that triggers
>>>>>>>> the
>>>>>>>> stopping, but I can only pass it the list once. So I don't think it
>>>>>>>> is
>>>>>>>> possible.
>>>>>>>
>>>>>>> It is, just pass some other object along which can call the method
>>>>>>> "after".
>>>>>>>
>>>>>>>> But if this made sense to anyone, and you have a suggestion I
>>>>>>>> would love it. Heres the full code: (but at the bottom, the Open
>>>>>>>> function
>>>>>>>> is
>>>>>>>> really the only thing that matters)
>>>>>>>>
>>>>>>>
>>>>>>> If you want help based on code, you have to post a short-enough code
>>>>>>> that demonstrates the problem.
>>>>>>>
>>>>>>>> from Tkinter import *
>>>>>>>> import time
>>>>>>>>
>>>>>>>> class BusyBar(Frame):
>>>>>>>> ...
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> -- Guilherme H. Polo Goncalves
>>>>>>> _______________________________________________
>>>>>>> Tkinter-discuss mailing list
>>>>>>> Tkinter-discuss at python.org
>>>>>>> http://mail.python.org/mailman/listinfo/tkinter-discuss
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> Okay, so I modified the bottom code to this:
>>>>>>
>>>>>> def Open(root):
>>>>>>
>>>>>>    bb = BusyBar(root, text='Grabbing Definitions')
>>>>>>    bb.pack(side=LEFT, expand=NO)
>>>>>>
>>>>>>
>>>>>>    def sleeper():
>>>>>>        root.update
>>>>>
>>>>> What if you change this to root.update() ?
>>>>>
>>>>>>        root.after(1, sleeper)
>>>>>
>>>>> after works with milliseconds, not seconds, be aware.
>>>>>
>>>>>>    bb.on()
>>>>>>    root.update_idletasks()
>>>>>>
>>>>>>    sleeper()
>>>>>>
>>>>>>    #for i in range(0, 100):
>>>>>>        #time.sleep(0.1)
>>>>>>        #root.update()
>>>>>>    bb.of()
>>>>>>
>>>>>> but it doesn't repeat. What am I missing?
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> -- Guilherme H. Polo Goncalves
>>>>> _______________________________________________
>>>>> Tkinter-discuss mailing list
>>>>> Tkinter-discuss at python.org
>>>>> http://mail.python.org/mailman/listinfo/tkinter-discuss
>>>>>
>>>>>
>>>>
>>>> Well, what was happening before is that the bar would just be at a
>>>> standstill. After making it update() it moved a little, but was just a
>>>> standstill at a different place, if that makes sense. Any more ideas?
>>>> heres
>>>> the code:
>>>>
>>>> def Open(root):
>>>>
>>>>    bb = BusyBar(root, text='Grabbing Definitions')
>>>>    bb.pack(side=LEFT, expand=NO)
>>>>
>>>>    bb.on()
>>>>    root.update_idletasks()
>>>>
>>>>    def sleeper():
>>>>        root.update()
>>>>        root.after(1, sleeper)
>>>
>>> Did you ignore my last email where I said after takes milliseconds,
>>> not seconds ? And this will forever, not what you want apparently.
>>>
>> 
>> I forgot a word there, "... And this will run forever ...", sorry
>> 
>>>>
>>>>    sleeper()
>>>>
>>>>    #for i in range(0, 100):
>>>>        #time.sleep(0.1)
>>>>        #root.update()
>>>>    bb.of()
>>>
>>> The code you have pasted in the last two emails don't show the problem
>>> you are having. I guess someone else will have to look at your entire
>>> code to give more help.
>>>
>> 
>> 
>> 
>> -- 
>> -- Guilherme H. Polo Goncalves
>> _______________________________________________
>> Tkinter-discuss mailing list
>> Tkinter-discuss at python.org
>> http://mail.python.org/mailman/listinfo/tkinter-discuss
>> 
>> 
> 
> 
I am not that familiar with teh after call, but when I change it to this:

def Open(root):

    bb = BusyBar(root, text='Grabbing Definitions')
    bb.pack(side=LEFT, expand=NO)
        
    bb.on()
    root.update_idletasks()
    
    def sleeper():
        root.update()
        root.after(100000000, sleeper())
        
    sleeper()

(notice the () after sleeper in after). It gives me this error. regardless
of the number in after. Anyways, I get this error

 root.update()
RuntimeError: maximum recursion depth exceeded
-- 
View this message in context: http://www.nabble.com/Stopping-a-for-loop-with-a-sleep%28%29-funciton-in-it-tp18391735p18397060.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.



More information about the Tkinter-discuss mailing list