0

I have a application with gtk window and socket connection as server. Client is sending data to this server with image and I am trying to show updated images sent via socket in gtk window. But this gtk window is constantly seems to be freezed. Any hint how to make it "not freezed" when program is running? Probably place somewhere in code something like threading... not sure.

import socket
import sys

import gtk, PIL.Image, time
from PIL import Image


# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 8888)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)


img_width = 180
img_height = 144
test = gtk.gdk.Pixbuf(
  gtk.gdk.COLORSPACE_RGB,
  False,
  8,
  img_width,
  img_height
)  
rowstride = test.get_rowstride()  #zistim si rowstride
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.show_all()


while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(100000) #100000
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall("OK") #data

                pixbuf = gtk.gdk.pixbuf_new_from_data(data, gtk.gdk.COLORSPACE_RGB, False, 8, img_width, img_height, rowstride)   #540 rowstride
                pixmap, mask = pixbuf.render_pixmap_and_mask()
                image = gtk.Image()
                image.set_from_pixmap(pixmap, mask)

                #window = gtk.Window(gtk.WINDOW_TOPLEVEL)
                window.add(image)     #image      data
                window.show_all()
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()
peter
  • 4,289
  • 12
  • 44
  • 67
  • all GUIs has "mainloop" / "event loop" which has to work all time to get events from system, sends events to widgets, redraw widgets, etc. If you run long-running function then "mainloop" doesn't works and it looks like window freeze. You can run your function in separated `thread`. – furas Dec 09 '17 at 02:18
  • BTW: see [simple example](https://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html#simple-example) in doc - program needs `Gtk.main()` to start "mainloop" / "event loop" but all code after `mainloop` is executed after you close window. You have to run your socket before `main()` in some thread. – furas Dec 09 '17 at 02:22
  • thank you pointing me right direction! – peter Dec 09 '17 at 08:36
  • Possible duplicate of [How to listen socket, when app is running in gtk.main()?](https://stackoverflow.com/questions/47265105/how-to-listen-socket-when-app-is-running-in-gtk-main) – liberforce Dec 11 '17 at 12:51

0 Answers0