# Tk delay

**URL:** <https://www.ruby-forum.com/t/tk-delay/186289>\
**Category:** Ruby\
**Created:** [April 3, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/tk-delay/186289 "2010-04-03T18:49:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 3, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/tk-delay/186289/1 "2010-04-03T18:49:09Z")

</div>

I am somewhat new to Ruby and Tk, I have made a couple of simple  
programs, but now I’m working on a Music Automation Program… like for  
a Radio Station… something that will play song after song, infinitely.  
Right now I need a means to delay the next song for the number of  
seconds that the current song is playing. I’ve heard of a Tk::after  
method, but I am having trouble understanding how it works.

Any help, links, or examples would be greatly appreciated…

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 3, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/tk-delay/186289/2 "2010-04-03T20:40:45Z")

</div>

Jesse J. wrote:

> Right now I need a means to delay the next song for the number of  
> seconds that the current song is playing.

I’m using the Snack audio toolkit for ruby, so I already know how long  
the song is… I don’t need to constantly check if the song is  
playing… I just need to have a block of code run after a set number of  
seconds.

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 4, 2010, 7:34am UTC](https://www.ruby-forum.com/t/tk-delay/186289/3 "2010-04-04T07:34:16Z")

</div>

On Sun, Apr 4, 2010 at 3:40 AM, Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Jesse J. wrote:
> 
> > Right now I need a means to delay the next song for the number of  
> > seconds that the current song is playing.
> 
> I’m using the Snack audio toolkit for ruby, so I already know how long  
> the song is… I don’t need to constantly check if the song is  
> playing… I just need to have a block of code run after a set number of  
> seconds.

In stdlib Tk:  
ruby -rtk -e ‘Tk.after(5000){ puts “hi” }; Tk.mainloop’

In ffi-tk:  
ruby -rffi-tk -e ‘Tk::After.ms(5000){ puts “hi” }; Tk.mainloop’

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 5, 2010, 12:07am UTC](https://www.ruby-forum.com/t/tk-delay/186289/4 "2010-04-05T00:07:18Z")

</div>

From: Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Tk delay  
Date: Sun, 4 Apr 2010 03:40:47 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I’m using the Snack audio toolkit for ruby, so I already know how long  
> the song is… I don’t need to constantly check if the song is  
> playing… I just need to have a block of code run after a set number of  
> seconds.

Snack toolkit can call a callback procedure at end of the sound.  
Isn’t it enough for your purpose?

I don’t know Snack for Ruby (rbsnack) library.  
Even if rbsnack doesn’t support such callback,  
you can call the feature of Tcl/Tk from Ruby/Tk  
(if your Tcl/Tk can load Snack, and your Ruby/Tk loads the Tcl/Tk’s  
libs).

For example,

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 5, 2010, 2:52am UTC](https://www.ruby-forum.com/t/tk-delay/186289/5 "2010-04-05T02:52:13Z")

</div>

Michael F. wrote:

> On Sun, Apr 4, 2010 at 3:40 AM, Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Jesse J. wrote:
> > 
> > > Right now I need a means to delay the next song for the number of  
> > > seconds that the current song is playing.
> > 
> > I’m using the Snack audio toolkit for ruby, so I already know how long  
> > the song is… I don’t need to constantly check if the song is  
> > playing… I just need to have a block of code run after a set number of  
> > seconds.
> 
> In stdlib Tk:  
> ruby -rtk -e ‘Tk.after(5000){ puts “hi” }; Tk.mainloop’
> 
> In ffi-tk:  
> ruby -rffi-tk -e ‘Tk::After.ms(5000){ puts “hi” }; Tk.mainloop’

Thanks, this works… but I can’t get it to loop! I’m going to try using  
this with a TkTimer widget… but is there perchance a loop option in  
this widget as well? Something like TkTimer’s loop?  
i.e. ‘TkTimer.start(5000, -1){puts “hi”}’

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 5, 2010, 3:57am UTC](https://www.ruby-forum.com/t/tk-delay/186289/6 "2010-04-05T03:57:53Z")

</div>

From: Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Tk delay  
Date: Mon, 5 Apr 2010 09:52:20 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Thanks, this works… but I can’t get it to loop! I’m going to try using  
> this with a TkTimer widget… but is there perchance a loop option in  
> this widget as well? Something like TkTimer’s loop?  
> i.e. ‘TkTimer.start(5000, -1){puts “hi”}’

interval argument of TkTimer can accept a proc object.  
The proc is called when decide the interval time for the next call.  
So, the proc must return a milisecond value of integer.

For example,

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 5, 2010, 3:28am UTC](https://www.ruby-forum.com/t/tk-delay/186289/7 "2010-04-05T03:28:02Z")

</div>

On Mon, Apr 5, 2010 at 9:52 AM, Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > playing… I just need to have a block of code run after a set number of  
> > > this widget as well? Something like TkTimer’s loop?  
> > > i.e. ‘TkTimer.start(5000, -1){puts “hi”}’

tick = lambda{  
puts “hi”  
Tk.after(5000, &tick)  
}  
Tk.after(5000, &tick)

---

<div class="post-metadata">

**Author:** ![jcubed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jcubed](https://www.ruby-forum.com/u/jcubed)\
**Post date:** [April 6, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/tk-delay/186289/8 "2010-04-06T21:49:46Z")

</div>

Michael F. wrote:

> On Mon, Apr 5, 2010 at 9:52 AM, Jesse J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > playing… I just need to have a block of code run after a set number of  
> > > > this widget as well? Something like TkTimer’s loop?  
> > > > i.e. ‘TkTimer.start(5000, -1){puts “hi”}’
> 
> tick = lambda{  
> puts “hi”  
> Tk.after(5000, &tick)  
> }  
> Tk.after(5000, &tick)

Thanks! Works perfectly!
