[CentralOH] How-To non-blocking GUI with Tkinter
pybokeh
pybokeh at gmail.com
Thu Mar 1 04:38:36 CET 2012
Hello,
I am new to GUI programming and threading in Tkinter and I am basically
trying to create a chat or instant messaging program for fun or as a
learning experience. The problem I am having is my while loop doesn't loop
through due to the mainloop() function since it doesn't return to the main
thread until the root is closed or exited. So what ends up happening is I
can receive an instant message only once from the client, but server can
send message ok and the client receives the message fine. I've been doing
a lot of searching and it appears I need to either run a separate thread
for the part of the program that is waiting for the data from the client.
I've also looked at tkinter.createfilehandler(), but I just don't know how
to implement this. Any help with this in terms of documentation or
reference material would be greatly appreciated. Thanks in advance!
- Daniel
Here's my server code (it is GUI):
from Tkinter import *
import socket
class TCP_Server_GUI(Frame):
def __init__(self, master):
self.server_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.max_data = 512
self.host = '127.0.0.1'
self.port = 1060
self.server_socket.bind((self.host,self.port))
self.server_socket.listen(5)
self.frame = Frame(master)
self.frame.pack()
self.lblServer = Label(self.frame, text="Server IP:")
self.lblServer.grid(row=0, column=0)
self.strServer = StringVar()
self.Server = Entry(self.frame, textvariable=self.strServer,
width=16, borderwidth=2)
self.Server.grid(row=0, column=1)
self.lblPort = Label(self.frame, text="Server Port:")
self.lblPort.grid(row=1, column=0)
self.strPort = StringVar()
self.Port = Entry(self.frame, textvariable=self.strPort, width=6,
borderwidth=2)
self.Port.grid(row=1, column=1)
self.lblHistory = Label(self.frame, text="Message History:")
self.lblHistory.grid(row=2, column=0)
self.History = Listbox(self.frame, height=5, width=50)
self.History.grid(row=3, columnspan=2)
self.sbar = Scrollbar(self.frame, orient=VERTICAL,
command=self.History.yview)
self.sbar.grid(row=3, column=2)
self.History['yscrollcommand']=self.sbar.set
self.lblMessage = Label(self.frame, text="Outgoing Message:")
self.lblMessage.grid(row=4, column=0)
self.strMessage = StringVar()
self.Message = Entry(self.frame, textvariable=self.strMessage,
borderwidth=2, width=50)
self.Message.grid(row=5, columnspan=2)
self.strServer.set(self.host)
self.strPort.set(self.port)
self.client_socket, self.client_address =
self.server_socket.accept()
def SendBtn(self):
self.History.insert(END,
self.host+':'+str(self.port)+'--->'+self.strMessage.get())
self.History.yview_scroll(1, "units")
self.client_socket.send(self.strMessage.get())
self.strMessage.set('')"""
if __name__ == "__main__":
def SendBtn():
app.History.insert(END,
app.host+':'+str(app.port)+'--->'+app.strMessage.get())
app.History.yview_scroll(1, "units")
app.client_socket.send(app.strMessage.get())
print "Server sent data..."
app.strMessage.set('')
root = Tk()
root.title("TCP Chat Server GUI")
app = TCP_Server_GUI(root)
app.btnSend = Button(app.frame,text="Send", command=SendBtn)
app.btnSend.grid(row=6, column=0)
def checkdata():
print "Checking for incoming message from client..."
data = app.client_socket.recv(app.max_data)
client_ip, client_port = app.client_address
app.History.insert(END,
client_ip+':'+str(client_port)+'--->'+str(data))
print "Inserted message into history..."
root.update()
print "Done checking for incoming data..."
while True:
checkdata()
print "Excecuting main loop"
root.mainloop()
Client code (it is not GUI, just console):
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("127.0.0.1", 1060))
while 1:
data = raw_input ( "SEND( TYPE q or Q to Quit):" )
if (data <> 'Q' and data <> 'q'):
client_socket.send(data)
else:
client_socket.send(data)
client_socket.close()
break;
server_message = client_socket.recv(512)
print "Message from server:", server_message
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20120229/3af39072/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TCP_Server_GUI.py
Type: text/x-python
Size: 2936 bytes
Desc: not available
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20120229/3af39072/attachment-0002.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tcpclient.py
Type: text/x-python
Size: 473 bytes
Desc: not available
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20120229/3af39072/attachment-0003.py>
More information about the CentralOH
mailing list