tkinter, sockets and threads together

Discussion in 'Python' started by Julia Goolia, Sep 11, 2003.

  1. Julia Goolia

    Julia Goolia Guest

    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
    Julia Goolia, Sep 11, 2003
    #1
    1. Advertising

  2. In article <>,
    (Julia Goolia) wrote:

    >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?


    For reading from a socket you can use file events. See Tkinter Summary
    at my site <http://www.astro.washington.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.TkSocket 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
    Russell E. Owen, Sep 11, 2003
    #2
    1. Advertising

  3. (Julia Goolia) writes:

    > 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?


    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
    Martin v. =?iso-8859-15?q?L=F6wis?=, Sep 11, 2003
    #3
  4. Richard Townsend, Sep 11, 2003
    #4
  5. Julia Goolia

    Steve Holden Guest

    "Julia Goolia" <> wrote in message
    news:...
    > 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!


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

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965

    recipe, which IIRC is pretty general.

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
    Steve Holden, Sep 12, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    0
    Views:
    441
    VisionSet
    Nov 11, 2005
  2. Replies:
    5
    Views:
    594
    Eric Brunel
    Feb 12, 2004
  3. Rod Stephenson
    Replies:
    0
    Views:
    445
    Rod Stephenson
    Jun 22, 2004
  4. Nathan Pinno
    Replies:
    1
    Views:
    373
  5. Nathan Pinno
    Replies:
    5
    Views:
    1,138
    Nathan Pinno
    Nov 19, 2005
Loading...

Share This Page