Python Tkinter socket.recv not receiving
Question: I am trying to make a chat program with Python Tkinter, but my recv function
_recvMSG()_ either doesn't receive anything or just doesn't print anything.

Could you help me fix the receiving problem? Change the code anyway you want.

    
    
    from Tkinter import *
    import easygui
    import socket
    import threading
    msgscount = 1
    
    setup = False
    
    def makeServer():
        global tcpclisock
        host = ''
        port = easygui.integerbox(msg='Enter a port...', title='Port', argUpperBound=100000)
        buffsize = 1024
        addr = (host, port)
        setup = True
    
        tcpsersock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcpsersock.bind(addr)
        tcpsersock.listen(5)
        tcpclisock, addr = tcpsersock.accept()
        easygui.msgbox(msg=('Connected from: ', addr), title='Connected')
    
    def connectServer():
        global tcpclisock
        setup = True
        host = easygui.choicebox(msg='Choose a host...', title='Choose Host', choices=('localhost'),buttons=('Select'))
    port = easygui.integerbox(msg='Enter a port...', title='Port', argUpperBound=100000)
    buffsize = 1024
    addr = (host, port)
    
    tcpclisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcpclisock.connect(addr)
    
    def sendMSG():
        global msgscount, tcpclisock
        try:
            msgs.insert(msgscount, ">>" + msg.get())
            data = msg.get()
            msg.delete(0,END)
            msgscount += 1
            tcpclisock.send(data)
        except:
            easygui.msgbox(msg='You need to connect to a server.', title='Error')
    
    
    def recvMSG():
        global msgscount,tcpclisock
        if setup == True:
            datar = tcpclisock.recv(buffsize)
            msgs.insert(msgscount, datar)
            msgscount += 1
            top.after(1000, recvMSG)
        else:
            answ = easygui.buttonbox(msg='Please join or create a server', title='Server', choices=("Create", 'Join'))
            if answ == "Create":
                makeServer()
            else:
                connectServer()
    
    
    top = Tk()
    top.geometry('640x650')
    
    msgs = Listbox(top, width=105, height=43)
    msgs.pack()
    
    msg = Entry(top, bd=2, width=60, font='Arial 12 bold')
    msg.pack(side=LEFT)
    
    sendbtn = Button(top, text='Send', font='Arial 15 bold',width=15, padx=5, pady=5,command=runSendMSG)
    sendbtn.pack(side=RIGHT)
    
    menubar = Menu(top)
    servermenu = Menu(menubar, tearoff=0)
    servermenu.add_command(label="Create Server", command=makeServer)
    servermenu.add_command(label="Join Server", command=connectServer)
    menubar.add_cascade(label='Server', menu=servermenu)
    
    top.config(menu=menubar)
    
    top.after(5000, recvMSG)
    mainloop()
    


Answer: I don't have `easygui` so I removed some code

    
    
    from Tkinter import *
    #import easygui
    import socket
    #import threading
    
    msgscount = 1
    
    setup = False
    buffsize = 1024
    
    def makeServer():
        global tcpclisock,setup,buffsize
        host = ''
        port = 9000
        buffsize = 1024
        addr = (host, port)
        setup = True
    
        print "makeServer():"
    
        tcpsersock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcpsersock.bind(addr)
        tcpsersock.listen(5)
        tcpclisock, addr = tcpsersock.accept()
        tcpclisock.setblocking(0)
        print 'Connected from: ', addr
        top.after(1000, recvMSG)
    
    def connectServer():
        global tcpclisock,setup,buffsize
        setup = True
        host = ''
        port = 9000
        buffsize = 1024
        addr = (host, port)
    
        tcpclisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        tcpclisock.connect(addr)
        print 'Connected to: ', addr
        tcpclisock.setblocking(0)
        top.after(1000, recvMSG)
    
    def sendMSG():
        print 'sendMSG():'
        global msgscount, tcpclisock,setup
        try:
            msgs.insert(msgscount, ">>" + msg.get())
            data = msg.get()
            msg.delete(0,END)
            msgscount += 1
            print tcpclisock.send(data)
        except:
            print 'You need to connect to a server.'
    
    
    def recvMSG():
        global msgscount,tcpclisock,setup,buffsize
        print 'recvMSG():', setup
        if setup == True:
            try:
                datar = tcpclisock.recv(buffsize)
                msgs.insert(msgscount, datar)
                msgscount += 1
            except:
                pass
            top.after(1000, recvMSG)
        else:
            pass
            answ = "Create"
            if answ == "Create":
                makeServer()
            else:
                connectServer()
    
    
    top = Tk()
    #top.geometry('640x650')
    
    msgs = Listbox(top, width=105, height=43)
    msgs.pack()
    
    msg = Entry(top, bd=2, width=60, font='Arial 12 bold')
    msg.pack()
    
    sendbtn = Button(top, text='Send', font='Arial 15 bold', width=15, padx=5, pady=5, command=sendMSG)
    sendbtn.pack()
    
    menubar = Menu(top)
    servermenu = Menu(menubar, tearoff=0)
    servermenu.add_command(label="Create Server", command=makeServer)
    servermenu.add_command(label="Join Server", command=connectServer)
    menubar.add_cascade(label='Server', menu=servermenu)
    
    top.config(menu=menubar)
    
    mainloop()
    

