from socket import *
from time import time, ctime
import thread 
import Queue

import os

class ThreadSafeConsole(Text):
    def __init__(self, master, **options):
        Text.__init__(self, master, **options)
        self.queue = Queue.Queue()
        self.update_me()
    def write(self, line):
        self.queue.put(line)
    def clear(self):
        self.queue.put(None)
    def update_me(self):
        try:
            while 1:
                line = self.queue.get_nowait()
                if line is None:
                    self.delete(1.0, END)
                else:
                    self.insert(END, str(line))
                self.see(END)
                self.update_idletasks()
        except Queue.Empty:
            pass
        self.after(100, self.update_me)


def pipeToWidget(mensagem, widget):
    widget.clear()
    while 1:
        line = input.readline()
        if not line:
            break
        widget.write(line)

def funcThread(widget):

    IP = 'xxx.xxx.x.x'#insert your ip
    PORT = 23456
    ADS = (IP, PORT)

    tcpsoc = socket(AF_INET, SOCK_STREAM)
    tcpsoc.bind(ADS)
    tcpsoc.listen(5)

    while 1:
            mensagem = "Waiting for connection"
            tcpcli, addr = tcpsoc.accept()
            print "connected from:", addr
            while 1:
                    mensagem = tcpcli.recv(1024)
                    if not mensagem : break
                    print mensagem
                    mensagem1 = raw_input(">>")
                    if mensagem1 == "q;t": break
                    tcpcli.send(mensagem1)
            tcpcli.close()
    tcpsoc.close()

    pipeToWidget(mensagem, widget)

# uber-main
root = Tk()
widget = ThreadSafeConsole(root)
widget.pack(side=TOP, expand=YES, fill=BOTH)
thread.start_new(funcThread, (widget,))

root.mainloop()

i´m trying to make a server in tkinter ... but i dont have any idea how make this... sorry my bad english ... and thank you for the help, the code below doesn´t work obviously i never try to handle threads in tkinter ...

thanks, i read this documentation, but i dont understand ... it´s not easy to understand and integrate in code

This article has been dead for over six months. Start a new discussion instead.