pygtk gui冻结而套接字刷新传入的数据

时间:2017-12-09 01:46:46

标签: python gtk pygtk

我有一个带有gtk窗口和套接字连接的应用程序作为服务器。客户端使用图像向此服务器发送数据,我试图在gtk窗口中显示通过套接字发送的更新图像。但是这个gtk窗口似乎不断被冻结。任何提示如何使它"不冻结"程序运行时?可能会在代码中的某处放置类似线程......不确定。

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()

0 个答案:

没有答案