Page 1 of 1

Python streaming data with GUI running

Posted: Thu May 25, 2006 10:23 am
by shiznatix
OK! So I have a program that will connect to a server through telnet and open a little GUI thing to input text. I want all the incoming stuff from the server to be printed to the terminal window while everything from the little GUI box goes to the server.

The box to server works just fine, the problem is that I don't know how to have a loop that gets the data from the server while running the GUI box. If anyone can help out that would be fantastic. here is the code I am struggling with:

Code: Select all

#! /usr/bin/env python

from Tkinter import *
import telnetlib

HOST = 'localhost'
PORT = 4000

telnet = telnetlib.Telnet(HOST, PORT)

def Display():
	global enText
	global telnet
	
	telnet.write(enText.get())
	print str(telnet.read_some())
	return True
	
root = Tk()
 
lbText = Label(root, text="Enter Text:")
lbText.grid(row=0, column=0)

enText = Entry(root)
enText.grid(row=0, column=1, columnspan=3)

btnDisplay = Button(root, text="Send", command=Display)
btnDisplay.grid(row=0, column=4)

root.mainloop()
[/syntax]

Posted: Thu May 25, 2006 10:41 am
by timvw
I'm not familiar with python but here are the two possible approaches:

- use non-blocking i/o (a function call to read/write immediately returns instead of waiting untill the work has been done)
- use threads (one to paint to gui, one to handle the io)

Posted: Thu May 25, 2006 11:19 am
by shiznatix
ahha I figured it out! I had to make a read function that does recursion with the GUI stuff. Heres the function

Code: Select all

def read():
	global telnet
	global root
	
	msg = telnet.read_very_eager()
		
	if msg:
		print str(msg)
	
	root.after(100, read)
then I just call read() right before I start the GUI and all is well. cool, awesome, super, ya[/syntax]