tkinter socket client ?

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

    #1

    tkinter socket client ?

    I have been looking through the previous posts - but with my lack of
    knowledge on the whole tkinter subject - I have no clue what to look
    for ...

    SO - can anyone please help with this ...?

    I have a python server that when it gets a connection from a client -
    it sends data back - quite a bit of it - in discreet parts - around
    1024 byte chunks ...

    I have created a tkinter client that makes a simple connect to the
    server. What I have a problem with is I need the client to write all
    the data to the Text() widget created in the GUI client - the Text()
    widget is created as :

    self.text=Text( self.center_fra me,background=' white')
    scroll=Scrollba r(self.center_f rame)
    self.text.confi gure(yscrollcom mand=scroll.set )

    self.text.pack( side=LEFT, fill=BOTH, expand=YES)
    scroll.pack(sid e=RIGHT,fill=Y)
    self.center_fra me.pack(side=RI GHT, expand=YES, fill=BOTH)


    I have a button with "Connect" written on it that connects to the
    server by calling :

    HOST = 'localhost'
    PORT = 9000
    global s
    for res in socket.getaddri nfo(HOST, PORT, socket.AF_UNSPE C,
    socket.SOCK_STR EAM):
    af, socktype, proto, canonname, sa = res
    try:
    s = socket.socket(a f, socktype, proto)
    except socket.error, msg:
    s = None
    continue
    try:
    s.connect(sa)
    except socket.error, msg:
    s.close()
    s = None
    continue
    break
    if s is None:
    print 'could not open socket'
    self.quitButton Click

    NOW - HOW do I get the server's sent data to continuiosly print in the
    Text() widget ?

    Many thank
    Tonino

  • Christos TZOTZIOY Georgiou

    #2
    Re: tkinter socket client ?

    On 20 Jan 2005 22:25:52 -0800, rumours say that "Tonino"
    <tonino.greco@g mail.com> might have written:

    [tkinter gui for a socket client, lots of data from the socket]
    [color=blue]
    >NOW - HOW do I get the server's sent data to continuiosly print in the
    >Text() widget ?[/color]

    You need to use:

    yourTextWidget. insert(Tkinter. END, data) # to insert the data
    yourRootWindow. update_idletask s() # to update the GUI
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...

    Comment

    • Tonino

      #3
      Re: tkinter socket client ?

      thanks for the reply .

      just one problem - I do not know how to sit in a "loop" accepting
      messages on the socket connection - writting them to the Text() widget
      - refreshing the the GUI - and then starting all over ....
      where do I put the loop for the socket ?

      Thanks
      Tonino

      Comment

      • Diez B. Roggisch

        #4
        Re: tkinter socket client ?

        > just one problem - I do not know how to sit in a "loop" accepting[color=blue]
        > messages on the socket connection - writting them to the Text() widget
        > - refreshing the the GUI - and then starting all over ....
        > where do I put the loop for the socket ?[/color]

        Another thread? Or use twisted, it comes with a tkinter-aware exec-loop:



        --
        Regards,

        Diez B. Roggisch

        Comment

        • Tonino

          #5
          Re: tkinter socket client ?

          thanks for the info - but I really do not want to learn twisted before
          I can understand Tkinter ;)
          another thread seems the way - will try that ...

          Thanks
          Tonino

          Comment

          • Neal  Norwitz

            #6
            Re: tkinter socket client ?

            You are probably looking for Tkinter.createf ilehandler(). Here are
            some snippets to get you started:

            tk_reactor = Tkinter._tkinte r
            self.sd = socket(AF_INET, SOCK_STREAM)
            self.sd.connect ((HOST, PORT))
            tk_reactor.crea tefilehandler(s elf.sd, Tkinter.READABL E,
            self.handle_inp ut)

            def handle_input(se lf, sd, mask):
            data = self.sd.recv(SI ZE)
            (Sorry if the formatting is busted, blame google groups.)

            HTH,
            Neal

            Comment

            • Tonino

              #7
              Re: tkinter socket client ?

              hi there ,

              yeah - had a look at createfilehandl er() - was a bit confusing - but
              your example helps ;)

              Thanks
              Tonino

              Comment

              • Russell E. Owen

                #8
                Re: tkinter socket client ?

                In article <1106319270.932 204.180590@c13g 2000cwb.googleg roups.com>,
                "Tonino" <tonino.greco@g mail.com> wrote:
                [color=blue]
                >yeah - had a look at createfilehandl er() - was a bit confusing - but
                >your example helps ;)[/color]

                Be warned that createfilehandl er does not work on Windows, though it
                works well on unix and MacOS X.

                So my suggestion is one to try any of these (any of which are preferable
                to threads):
                - file handlers for non-Windows code
                - use tcl sockets for cross-platform code.
                - use Twisted Framework (some work to learn, but supposedly very solid;
                I confess I've never used it myself).

                There is a bit of info on the first two options (including a link to the
                RO package that includes a python interface to tcl sockets) here:

                <http://www.astro.washi ngton.edu/rowen/TkinterSummary. html#FileHandle rs>

                -- Russell

                Comment

                • Tonino

                  #9
                  Re: tkinter socket client ?

                  Hi,

                  thanks for this info - I had to abandon the createfilehandl er() method
                  as it is not supported in windows and the GUI "might" be used there at
                  some time ...

                  So - I went the threading route - works well - for now - so I will
                  stick to it ...

                  BUT - the next question:
                  In the Text() widget - why - when the text scrolls off the screen -
                  does the window not follow it ?

                  I have added a scrollbar to it :

                  self.center_fra me = Frame(self.top_ frame, background="tan ",
                  relief=RIDGE)

                  self.text=Text( self.center_fra me,background=' white')
                  scroll=Scrollba r(self.center_f rame)
                  self.text.confi gure(yscrollcom mand=scroll.set )

                  self.text.pack( side=LEFT, fill=BOTH, expand=YES)
                  scroll.pack(sid e=RIGHT,fill=Y)
                  self.center_fra me.pack(side=RI GHT, expand=YES, fill=BOTH)


                  but the window does not scroll to follow the text ?
                  Any ideas ?

                  Thanks
                  Tonino

                  Comment

                  • Martin Franklin

                    #10
                    Re: tkinter socket client ?

                    Tonino wrote:[color=blue]
                    > Hi,
                    >
                    > thanks for this info - I had to abandon the createfilehandl er() method
                    > as it is not supported in windows and the GUI "might" be used there at
                    > some time ...
                    >
                    > So - I went the threading route - works well - for now - so I will
                    > stick to it ...
                    >
                    > BUT - the next question:
                    > In the Text() widget - why - when the text scrolls off the screen -
                    > does the window not follow it ?
                    >
                    > I have added a scrollbar to it :
                    >
                    > self.center_fra me = Frame(self.top_ frame, background="tan ",
                    > relief=RIDGE)
                    >
                    > self.text=Text( self.center_fra me,background=' white')
                    > scroll=Scrollba r(self.center_f rame)
                    > self.text.confi gure(yscrollcom mand=scroll.set )
                    >
                    > self.text.pack( side=LEFT, fill=BOTH, expand=YES)
                    > scroll.pack(sid e=RIGHT,fill=Y)
                    > self.center_fra me.pack(side=RI GHT, expand=YES, fill=BOTH)
                    >
                    >
                    > but the window does not scroll to follow the text ?
                    > Any ideas ?
                    >[/color]

                    This is the default behavior of the Text widget. You have two options
                    (as I see it) one, put the new text at the top of the Text widget
                    textwidget.inse rt(0, "your new text") or two, use the yview_pickplace
                    method to move the view down every time you insert text
                    textwidget.yvie w_pickplace('en d')

                    I wrote a sub-class of the ScrolledText widget to do just this
                    I gave it a write method - so I could re-direct stdout to it - and also
                    called yview_pickplace in that method.

                    HTH
                    Martin.

                    Comment

                    • Russell E. Owen

                      #11
                      Re: tkinter socket client ?

                      In article <1106659122.977 876.234250@z14g 2000cwz.googleg roups.com>,
                      "Tonino" <tonino.greco@g mail.com> wrote:
                      [color=blue]
                      >thanks for this info - I had to abandon the createfilehandl er() method
                      >as it is not supported in windows and the GUI "might" be used there at
                      >some time ...
                      >
                      >So - I went the threading route - works well - for now - so I will
                      >stick to it ...
                      >
                      >BUT - the next question:
                      >In the Text() widget - why - when the text scrolls off the screen -
                      >does the window not follow it ?
                      >
                      >I have added a scrollbar to it :
                      >
                      >self.center_fr ame = Frame(self.top_ frame, background="tan ",
                      >relief=RIDGE )
                      >
                      >self.text=Text (self.center_fr ame,background= 'white')
                      >scroll=Scrollb ar(self.center_ frame)
                      >self.text.conf igure(yscrollco mmand=scroll.se t)
                      >
                      >self.text.pack (side=LEFT, fill=BOTH, expand=YES)
                      >scroll.pack(si de=RIGHT,fill=Y )
                      >self.center_fr ame.pack(side=R IGHT, expand=YES, fill=BOTH)
                      >
                      >
                      >but the window does not scroll to follow the text ?
                      >Any ideas ?[/color]

                      That's just how it works. But when you append text you can easily tell
                      the text widget to display it, e.g. using "see". Here is the code I use
                      (from RO.Wdg.LogWdg.p y), which has these useful features:
                      - auto-scrolls only if the user is already scrolled to the of text (so
                      if a user is staring at some older data, it won't be jerked out from
                      under them)
                      - deletes excess text.

                      def addOutput(self, astr, category=None):
                      """Add a line of data to the log.

                      Inputs:
                      - astr: the string to append. If you want a newline, specify the \n
                      yourself.
                      - category: name of category or None if no category
                      """
                      # set auto-scroll flag true if scrollbar is at end
                      # there are two cases that indicate auto-scrolling is wanted:
                      # scrollPos[1] = 1.0: scrolled to end
                      # scrollPos[1] = scrollPos[0]: window has not yet been painted
                      scrollPos = self.yscroll.ge t()
                      doAutoScroll = scrollPos[1] == 1.0 or scrollPos[0] == scrollPos[1]
                      if category:
                      self.text.inser t("end", astr, (category,))
                      else:
                      self.text.inser t("end", astr)
                      extraLines = int(float(self. text.index("end ")) - self.maxLineInd ex)
                      if extraLines > 0:
                      self.text.delet e("1.0", str(extraLines) + ".0")
                      if doAutoScroll:
                      self.text.see(" end")

                      -- Russell

                      Comment

                      • Tonino

                        #12
                        Re: tkinter socket client ?

                        great - thanks ;)

                        Tonino

                        Comment

                        • Pedro Werneck

                          #13
                          Re: tkinter socket client ?

                          On Tue, 25 Jan 2005 13:44:32 +0000
                          Martin Franklin <mfranklin1@gat wick.westerngec o.slb.com> wrote:
                          [color=blue][color=green]
                          > > thanks for this info - I had to abandon the createfilehandl er() method
                          > > as it is not supported in windows and the GUI "might" be used there at
                          > > some time ...[/color][/color]

                          Take a look here: http://www.pythonbrasil.com.br/moin....ketsComTkinter

                          Comment

                          Working...