tkinter, sockets and threads together

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julia Goolia

    tkinter, sockets and threads together

    hello,

    i read that it is bad to use threads with tkinter. so my question is
    how does one create a gui program with sockets? at one point you have
    to call mainloop() which does not return. then you are not free to do
    stuff with the sockets.

    what's up with that?

    thanks so much for your time!
    julia
  • Martin v. Löwis

    #2
    Re: tkinter, sockets and threads together

    juliagoolia301@ hotmail.com (Julia Goolia) writes:
    [color=blue]
    > i read that it is bad to use threads with tkinter. so my question is
    > how does one create a gui program with sockets? at one point you have
    > to call mainloop() which does not return. then you are not free to do
    > stuff with the sockets.
    >
    > what's up with that?[/color]

    On Unix, you can use Tk file handlers for sockets, i.e. have Tcl
    select(2) not only on the connection to the X11 server (which is a
    socket itself), but also select on your sockets. This is readily
    supported in _tkinter.

    On Windows, you have no choice but to use threads. If the socket
    processing threads have no relationship to the Tk thread, everything
    is fine. However, in many cases, interaction of some kind is
    needed. In this case, it is ok to call Tk methods from other threads,
    as long as they translate to "fast" commands, i.e. commands that only
    modify state, but don't block - you should not run the mainloop in
    multiple threads.

    Regards,
    Martin

    Comment

    • Russell E. Owen

      #3
      Re: tkinter, sockets and threads together

      In article <79ad5955.03091 11133.5f6bf26f@ posting.google. com>,
      juliagoolia301@ hotmail.com (Julia Goolia) wrote:
      [color=blue]
      >hello,
      >
      >i read that it is bad to use threads with tkinter. so my question is
      >how does one create a gui program with sockets? at one point you have
      >to call mainloop() which does not return. then you are not free to do
      >stuff with the sockets.
      >
      >what's up with that?[/color]

      For reading from a socket you can use file events. See Tkinter Summary
      at my site <http://www.astro.washi ngton.edu/owen/> for the basics.

      To avoid blocking while writing (i.e.queue up the output data and write
      it as the network permits), I ended up using threads. There are probably
      other options.

      The package "RO Util", available at th web site mentioned above includes
      a simple class RO.Comm.TkSocke t that handles input and output nicely,
      while hiding the details of file events and the writing thread.

      You could also use the Twisted Framework. It is reportedly well done and
      integrate nicely with Tkinter and other frameworks. I can't speak from
      personal experience as I've not yet tried it.

      -- Russell

      Comment

      • Richard Townsend

        #4
        Re: tkinter, sockets and threads together

        > i read that it is bad to use threads with tkinter. so my question is[color=blue]
        > how does one create a gui program with sockets? at one point you have
        > to call mainloop() which does not return. then you are not free to do
        > stuff with the sockets.
        >[/color]

        Take a look at

        iting-for-i-o


        RT



        Comment

        • Steve Holden

          #5
          Re: tkinter, sockets and threads together

          "Julia Goolia" <juliagoolia301 @hotmail.com> wrote in message
          news:79ad5955.0 309111133.5f6bf [email protected] gle.com...[color=blue]
          > hello,
          >
          > i read that it is bad to use threads with tkinter. so my question is
          > how does one create a gui program with sockets? at one point you have
          > to call mainloop() which does not return. then you are not free to do
          > stuff with the sockets.
          >
          > what's up with that?
          >
          > thanks so much for your time![/color]

          Well, one page worth reading would be Jacob Hallén's



          recipe, which IIRC is pretty general.

          regards
          --
          Steve Holden http://www.holdenweb.com/
          Python Web Programming http://pydish.holdenweb.com/pwp/




          Comment

          Working...