While in a tk mainloop

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

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.

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

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.

 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 :frowning: 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 :slight_smile:

Many thanks

Stuart

:slight_smile: 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

:slight_smile: 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs