tkinter socket client ?

T

Tonino

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
 
C

Christos TZOTZIOY Georgiou

On 20 Jan 2005 22:25:52 -0800, rumours say that "Tonino"
<[email protected]> 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
 
T

Tonino

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
 
T

Tonino

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
 
N

Neal Norwitz

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
 
T

Tonino

hi there ,

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

Thanks
Tonino
 
R

Russell E. Owen

"Tonino said:
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
 
T

Tonino

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
 
M

Martin Franklin

Tonino said:
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.
 
R

Russell E. Owen

"Tonino said:
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 ?

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top