This is my code for a chat client. I create a simple UI with a text box for viewing the chat and an entry widget to write messages. There's a button called 'Connect', when i click this i start a new thread which does the connection and keeps receiving messages n writing them to the text box. The 'run' method also checks the screen state and if it's not in focus or minimized then it plays a sound.
my problem is that i dont want to play a sound but rather toggle the 'title string' or change the color of the root widget, basically somekind of visual indication that a new message has come. i tried a lot of things but as soon i try to do anythin in this thread the application just hangs.
please c the code and suggest something.
from Tkinter import *
import socket
import threading
import tkMessageBox
import winsound
import re
import pdb
#pdb.set_trace()
class myUIChat(Frame,threading.Thread):
def __init__(self,par,wid,ht):
Frame.__init__(self,master=par,width=wid,height=ht)
threading.Thread.__init__(self)
self.parent = par
self.threadCount = 1
self.connect()
def connectToChat(self):
try:
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clientSocket.connect(('',7500))
self.connectButton.config(text='Disconnect',command=self.closeConnection)
self.startRecvThread()
except:
tkMessageBox.showinfo(message='Server down hai bidu... din bhar bas chatting hi karni hai tumhe')
def connect(self):
self.startChatWindow()
def startChatWindow(self):
#draw the chat window
self.scrollbar = Scrollbar(self)
self.scrollbar.grid(row=3,column=3)
self.connectButton = Button(self,text='Connect To Chat',command=self.connectToChat)
self.connectButton.grid(row=2,column=2)
self.textWidget = Text(self,yscrollcommand=self.scrollbar.set)
self.textWidget.grid(row=3,column=2)
self.textWidget.bind('<Insert>',self.showWin)
self.scrollbar.config(command=self.textWidget.yview)
#self.textWidget.config(state=DISABLED)
self.entryWidget = Entry(self)
self.entryWidget.grid(row=5,column=2)
self.entryWidget.bind('<Return>',self.sendMessage)
self.entryWidget.focus()
def showWin(self,event):
print 'showin'
def sendMessage(self,event):
try:
self.outMsg = self.entryWidget.get()
self.clientSocket.send(self.outMsg)
self.entryWidget.delete(0,END)
except:
tkMessageBox.showinfo(message='kuch gadbad hai.. baad main try karo, thodi der kaam kar lo')
self.closeConnection()
def startRecvThread(self):
self.start()
def run(self):
pdb.set_trace()
#try:
print 'main thread'
while 1:
message = self.clientSocket.recv(1000)
message = message + '\n'
if message.startswith('qwedfgbnm12345'):
message = re.sub('qwedfgbnm12345','',message)
self.textWidget.tag_config('fg',foreground='red')
self.textWidget.insert(INSERT,message,'fg')
continue
self.textWidget.insert(INSERT,message)
self.textWidget.see(END)
if self.parent.state() == 'iconic' or self.textWidget.focus_displayof() == None:
self.changeTitle()
winsound.PlaySound('complete.wav',winsound.SND_ALIAS)
## except:
## pass
def changeTitle(self):
#anythin meaninful i try to do here hangs the application
print self.parent.frame()
def closeConnection(self):
self.clientSocket.send('exit')
self.clientSocket.shutdown(socket.SHUT_RDWR)
self.clientSocket.close()
self.parent.destroy()
def main():
try:
root = Tk()
root.title('ChAt MaDI')
label = Label(root,text='hello',bg='yellow')
label.pack()
application = myUIChat(root,20,20)
application.pack()
root.mainloop()
root.destroy()
except:
pass
if __name__ == '__main__':
main()