Python streaming data with GUI running

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Python streaming data with GUI running

Post 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]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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]
Post Reply