[Tutor] A thread gui chat problem
Daryl G
gman_95@hotmail.com
Wed, 04 Jul 2001 22:37:10 -0000
Hi,
I seem to be having a problem with threading in my chat gui client program
The thread run method only accepts two recieve mesages and thats it and I
don't see why it does that. If I change the code in the run method to just
add a number to itself it works fine.
The chat server is a script I got from strout.net and sends messages it
receives to all clients.
What am I doing wrong?
Thanks
Daryl
#code
import threading, time
from Tkinter import *
import string
import sys
from socket import *
#Gui Class
class App:
def __init__(self, master):
#initialize socket variables for theclient
self.HOST='localhost'
self.BUFSIZ = 1024
self.PORT=4000
self.ADDR= (self.HOST, self.PORT)
self.tcpCliSock= socket(AF_INET, SOCK_STREAM)
self.tcpCliSock.connect(self.ADDR)
#initalize Gui
self.senddata=StringVar()
self.recdata=StringVar()
self.frame = Frame(master)
self.frame.pack()
self.send=Button(self.frame, text='Send', command=self.sendData)
self.send.pack(side=LEFT)
self.sendtext=Entry(self.frame, width=50,
textvariable=self.senddata)
self.sendtext.pack(side=LEFT)
self.rectext=Entry(self.frame, width=50,
textvariable=self.recdata)
self.rectext.pack(side=RIGHT)
#display text in gui
self.senddata.set('send chat area')
self.recdata.set('receive area')
def sendData(self):
str= self.senddata.get()
self.tcpCliSock.send(str)
#display received message in text entry rectext widget
def recData(self, data):
self.data=data
self.recdata.set(data)
#threading class module for receiving threads and calling recData method in
App
class msgThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
#add=1
while 1: #I get the same thing both with and without a while loop
#print add
data=app.tcpCliSock.recv(1024)
app.recdata.set(data)
#print data
#add=add+1
#time.sleep(1)
#print 'sleep'
root = Tk()
classtitle="chater"
root.title(classtitle)
app = App(root)
#Screen resolution must be set to 1024X768 or Gui will be off the screen if
#the resolution is smaller
root.geometry('%dx%d+%d+%d' % (660,20,210,690))
thread= msgThread()
thread.start()
root.mainloop()
app.tcpCliSock.close()
#end
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com