# While in a tk mainloop

**URL:** <https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168>\
**Category:** Ruby\
**Created:** [June 22, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168 "2006-06-22T18:49:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/1 "2006-06-22T18:49:30Z")

</div>

Hi all

How can I run a while loop in a Tk.mainloop?

If I run the while loop before calling Tk.mainloop and Tk doesn’t show  
and if I start Tk.mainloop before the while loop the while loop doesn’t  
start until the tk box closes, how do I run the while loop and let it  
continue by its self so tk can run?

Maybe some sort of thread?

Many thanks

Stuart

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/2 "2006-06-22T18:57:01Z")

</div>

Stuart B. wrote:

> Hi all
> 
> How can I run a while loop in a Tk.mainloop?

What exactly are you trying to do inside the while loop?  
depending on what you are trying to do more than likely there is a way  
to do it without the loop. I might be able to help if i know more about  
what you are trying to do.

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/3 "2006-06-22T19:33:25Z")

</div>

> What exactly are you trying to do inside the while loop?  
> depending on what you are trying to do more than likely there is a way  
> to do it without the loop. I might be able to help if i know more about  
> what you are trying to do.

Hi

Sure,

server = TCPServer.new(‘0.0.0.0’, 8080)  
while (session = server.accept)  
Thread.new{MainServer.handle\_connection(session)}  
end

The while loop is for new connection and the Tk loop would be to show  
the status

Many thanks

Stuart

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/4 "2006-06-22T19:43:01Z")

</div>

Stuart B. wrote:

> > What exactly are you trying to do inside the while loop?  
> > depending on what you are trying to do more than likely there is a way  
> > to do it without the loop. I might be able to help if i know more about  
> > what you are trying to do.
> 
> Hi
> 
> Sure,
> 
> server = TCPServer.new(‘0.0.0.0’, 8080)  
> while (session = server.accept)  
> Thread.new{MainServer.handle\_connection(session)}  
> end
> 
> The while loop is for new connection and the Tk loop would be to show  
> the status
> 
> Many thanks
> 
> Stuart

Well the only thing I can think of is to incapsulate it inside a class  
and initialize method like

```
 class foo
   def initialize
     #your tk code and while loop
   end
 end
 foo.new
 Tk.mainloop

```

that way everything gets read and the while loop won’t start till the  
Tk.mainloop is called. This should work if you adapt it right,  
hopefully.  
Hope this helps.

Tait P.

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/5 "2006-06-22T20:44:06Z")

</div>

> ```
> class foo
> def initialize
> #your tk code and while loop
> end
> end
> foo.new
> Tk.mainloop
> 
> ```

> Tait P.

Hi

Thanks, however, this does not work ☹ it only does the same as before,  
what if I start the Tk in a new thread? would this let it run  
independent of the main programme and then pass control back so the  
while loop can run?

I will try this 🙂

Many thanks

Stuart

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/6 "2006-06-22T20:47:55Z")

</div>

🙂 Yippee!

It worked, the is below

root = TkRoot.new { title “Proxy Server” }  
Thread.new{Tk.mainloop}  
server = TCPServer.new(‘0.0.0.0’, 8080)  
while (session = server.accept)  
Thread.new{MainServer.handle\_connection(session)}  
end

this creates a new thread for the Tk box and then lets the while loop  
carry on

Many thanks for your help all

Stuart

---

<div class="post-metadata">

**Author:** ![stuartbrand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@stuartbrand](https://www.ruby-forum.com/u/stuartbrand)\
**Post date:** [June 22, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/while-in-a-tk-mainloop/64168/7 "2006-06-22T20:48:05Z")

</div>

🙂 Yippee!

It worked, the code is below

root = TkRoot.new { title “Proxy Server” }  
Thread.new{Tk.mainloop}  
server = TCPServer.new(‘0.0.0.0’, 8080)  
while (session = server.accept)  
Thread.new{MainServer.handle\_connection(session)}  
end

this creates a new thread for the Tk box and then lets the while loop  
carry on

Many thanks for your help all

Stuart
