
July 18th, 2005, 09:30 PM
| | | 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_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 |

July 18th, 2005, 09:30 PM
| | | Re: tkinter socket client ?
On 20 Jan 2005 22:25:52 -0800, rumours say that "Tonino"
<tonino.greco@gmail.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_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... | 
July 18th, 2005, 09:30 PM
| | | 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 | 
July 18th, 2005, 09:30 PM
| | | 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: http://twistedmatrix.com/documents/h...eactor#auto16:
--
Regards,
Diez B. Roggisch | 
July 18th, 2005, 09:30 PM
| | | 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 | 
July 18th, 2005, 09:30 PM
| | | Re: tkinter socket client ?
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 | 
July 18th, 2005, 09:30 PM
| | | Re: tkinter socket client ?
hi there ,
yeah - had a look at createfilehandler() - was a bit confusing - but
your example helps ;)
Thanks
Tonino | 
July 18th, 2005, 09:37 PM
| | | Re: tkinter socket client ?
In article <1106319270.932204.180590@c13g2000cwb.googlegroups .com>,
"Tonino" <tonino.greco@gmail.com> wrote:
[color=blue]
>yeah - had a look at createfilehandler() - was a bit confusing - but
>your example helps ;)[/color]
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 | 
July 18th, 2005, 09:39 PM
| | | Re: tkinter socket client ?
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 | 
July 18th, 2005, 09:39 PM
| | | Re: tkinter socket client ?
Tonino wrote:[color=blue]
> 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 ?
>[/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.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. | 
July 18th, 2005, 09:40 PM
| | | Re: tkinter socket client ?
In article <1106659122.977876.234250@z14g2000cwz.googlegroups .com>,
"Tonino" <tonino.greco@gmail.com> wrote:
[color=blue]
>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 ?[/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.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 | 
July 18th, 2005, 09:45 PM
| | | Re: tkinter socket client ?
great - thanks ;)
Tonino | 
July 18th, 2005, 09:47 PM
| | | Re: tkinter socket client ?
On Tue, 25 Jan 2005 13:44:32 +0000
Martin Franklin <mfranklin1@gatwick.westerngeco.slb.com> wrote:
[color=blue][color=green]
> > 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 ...[/color][/color]
Take a look here: http://www.pythonbrasil.com.br/moin....ketsComTkinter | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,414 network members.
|