我正在做一个学习项目,我将在其中获取远程计算机的屏幕截图并通过套接字发送。该屏幕快照图像显示在接收数据的另一台计算机上。到目前为止,我已经能够获取屏幕截图并将其显示在GUI窗口中。但是,当我为tkinter窗口添加更新并重复发送屏幕截图图像时,我得到了一个空的Tk窗口。 这是我的服务器计算机代码:
import pyautogui
from socket import *
import io, pickle
from tkinter import *
from PIL.ImageTk import PhotoImage
from PIL import ImageOps
port = 50004
host = ''
sock = socket(AF_INET, SOCK_STREAM)
sock.bind((host, port))
sock.listen(5)
conn, addr = sock.accept()
data = conn.recv(1024)
print(data)
conn.send(b'Bingo')
def img_getter(connect):
file = io.BytesIO()
while True:
bytes = connect.recv(1024)
if not bytes: break
file.write(bytes)
img = pickle.loads(file.getvalue())
file.close()
return img
root = Tk()
frm = Frame(root)
frm.pack(expand=YES, fill=BOTH)
label = Label(frm)
label.pack(expand=YES, fill=BOTH)
def img_changer():
global label, conn, frm, img_getter
img = img_getter(conn)
img_scaled = ImageOps.scale(img, 0.7)
imgobj = PhotoImage(img_scaled, master=frm)
label.config(image=imgobj)
label.after(6000, img_changer)
img_changer()
root.mainloop()
这是发送屏幕截图的信誉计算机的代码:
import pyautogui, pickle
from socket import *
import time
port = 50004
host = '192.168.0.102'
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port))
sock.send(b'Connection is estamlished')
data = sock.recv(1024)
print(data)
while True:
img = pyautogui.screenshot()
sock.sendall(pickle.dumps(img))
有人可以帮助我在代码中发现我的错误或建议以其他方式执行相同过程的方法吗?