By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

tkinter socket client ?

P: n/a
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_frame,background='white ')
scroll=Scrollbar(self.center_frame)
self.text.configure(yscrollcommand=scroll.set)

self.text.pack(side=LEFT, fill=BOTH, expand=YES)
scroll.pack(side=RIGHT,fill=Y)
self.center_frame.pack(side=RIGHT, 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.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, 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.quitButtonClick

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

Many thank
Tonino

Jul 18 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On 20 Jan 2005 22:25:52 -0800, rumours say that "Tonino"
<to**********@gmail.com> might have written:

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


You need to use:

yourTextWidget.insert(Tkinter.END, data) # to insert the data
yourRootWindow.update_idletasks() # 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...
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
> 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 ?


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

http://twistedmatrix.com/documents/h...eactor#auto16:

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

P: n/a
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

Jul 18 '05 #5

P: n/a
You are probably looking for Tkinter.createfilehandler(). Here are
some snippets to get you started:

tk_reactor = Tkinter._tkinter
self.sd = socket(AF_INET, SOCK_STREAM)
self.sd.connect((HOST, PORT))
tk_reactor.createfilehandler(self.sd, Tkinter.READABLE,
self.handle_input)

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

HTH,
Neal

Jul 18 '05 #6

P: n/a
hi there ,

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

Thanks
Tonino

Jul 18 '05 #7

P: n/a
In article <11**********************@c13g2000cwb.googlegroups .com>,
"Tonino" <to**********@gmail.com> wrote:
yeah - had a look at createfilehandler() - was a bit confusing - but
your example helps ;)


Be warned that createfilehandler 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.washington.edu/rowen/TkinterSummary.html#FileHandlers>

-- Russell
Jul 18 '05 #8

P: n/a
Hi,

thanks for this info - I had to abandon the createfilehandler() 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_frame = Frame(self.top_frame, background="tan",
relief=RIDGE)

self.text=Text(self.center_frame,background='white ')
scroll=Scrollbar(self.center_frame)
self.text.configure(yscrollcommand=scroll.set)

self.text.pack(side=LEFT, fill=BOTH, expand=YES)
scroll.pack(side=RIGHT,fill=Y)
self.center_frame.pack(side=RIGHT, expand=YES, fill=BOTH)
but the window does not scroll to follow the text ?
Any ideas ?

Thanks
Tonino

Jul 18 '05 #9

P: n/a
Tonino wrote:
Hi,

thanks for this info - I had to abandon the createfilehandler() 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_frame = Frame(self.top_frame, background="tan",
relief=RIDGE)

self.text=Text(self.center_frame,background='white ')
scroll=Scrollbar(self.center_frame)
self.text.configure(yscrollcommand=scroll.set)

self.text.pack(side=LEFT, fill=BOTH, expand=YES)
scroll.pack(side=RIGHT,fill=Y)
self.center_frame.pack(side=RIGHT, expand=YES, fill=BOTH)
but the window does not scroll to follow the text ?
Any ideas ?


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.insert(0, "your new text") or two, use the yview_pickplace
method to move the view down every time you insert text
textwidget.yview_pickplace('end')

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.

Jul 18 '05 #10

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Tonino" <to**********@gmail.com> wrote:
thanks for this info - I had to abandon the createfilehandler() 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_frame = Frame(self.top_frame, background="tan",
relief=RIDGE)

self.text=Text(self.center_frame,background='whit e')
scroll=Scrollbar(self.center_frame)
self.text.configure(yscrollcommand=scroll.set)

self.text.pack(side=LEFT, fill=BOTH, expand=YES)
scroll.pack(side=RIGHT,fill=Y)
self.center_frame.pack(side=RIGHT, expand=YES, fill=BOTH)
but the window does not scroll to follow the text ?
Any ideas ?


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.py), 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.get()
doAutoScroll = scrollPos[1] == 1.0 or scrollPos[0] == scrollPos[1]
if category:
self.text.insert("end", astr, (category,))
else:
self.text.insert("end", astr)
extraLines = int(float(self.text.index("end")) - self.maxLineIndex)
if extraLines > 0:
self.text.delete("1.0", str(extraLines) + ".0")
if doAutoScroll:
self.text.see("end")

-- Russell
Jul 18 '05 #11

P: n/a
great - thanks ;)

Tonino

Jul 18 '05 #12

P: n/a
On Tue, 25 Jan 2005 13:44:32 +0000
Martin Franklin <mf********@gatwick.westerngeco.slb.com> wrote:
thanks for this info - I had to abandon the createfilehandler() method
as it is not supported in windows and the GUI "might" be used there at
some time ...


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

Jul 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.