import json import os import socket import struct import PySimpleGUI as sg def recvdata(conn, filepath): header_size = struct.unpack('i', conn.recv(4))[0] header_bytes = conn.recv(header_size) header_json = header_bytes.decode('utf-8') header_dic = json.loads(header_json) content_len = header_dic['contentlen'] content_name = header_dic['contentname'] recv_len = 0 pdf = os.path.join(filepath, content_name) with open(pdf, 'wb') as file: while recv_len < content_len: correntrecv = conn.recv(1024 * 1000) file.write(correntrecv) recv_len += len(correntrecv) def senddata(conn, path, librarynum, is_use_balance, is_use_max): name = os.path.basename(os.path.realpath(path)) try: with open(path, 'rb') as file: content = file.read() headerdic = dict( contentlen=len(content), contentname=name, librarynum=librarynum, is_use_balance=is_use_balance, is_use_max=is_use_max ) headerjson = json.dumps(headerdic) headerbytes = headerjson.encode('utf-8') headersize = len(headerbytes) conn.send(struct.pack('i', headersize)) conn.send(headerbytes) conn.sendall(content) except ConnectionResetError: print('不存在这个文件!') def connect(): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) dest_ip = '192.168.38.90' dest_port = int(8191) client.connect((dest_ip, dest_port)) return client def transclient(sendfile, resfile, librarynum, is_use_balance, is_use_max): conn = connect() senddata(conn, sendfile, librarynum, is_use_balance, is_use_max) recvdata(conn, resfile) def make_gui(): sg.theme('DarkBlack1') layout = [ [sg.Text('排样管数'), sg.Spin([i for i in range(150)], initial_value=130, size=(3, 1), key='_LIBRARYNUM_')], [sg.Text()], [sg.Text('使用平衡文库'), sg.Radio("是", "is_use_balance", key='is_use_balance', default=True), sg.Radio("否", "is_use_balance", key='is_not_use_balance')], [sg.Text('使用扩容平衡性'), sg.Radio("是", "is_use_max", key='is_use_max'), sg.Radio("否", "is_use_max", key='is_not_use_max', default=True)], [sg.Text()], [ sg.Text('导入排样excel')], [ sg.Input(key='_FILE1_'), sg.FileBrowse('选择文件')], [sg.Text()], [ sg.Text('生成排样位置')], [ sg.Input(key='_FILE2_'), sg.FolderBrowse('选择文件夹')], [sg.Text()], [ sg.OK('生成'), sg.Cancel('取消')]] # iconpath = os.path.join(os.path.abspath(sys.path[0]), 'other', 'icon.ico') window = sg.Window('解码排样T7程序', layout, font='Helvetica 11', icon=r'D:\project\autulayout\other\icon.ico') while True: event, values = window.read() is_use_balance = 1 if values['is_use_balance'] else 0 is_use_max = 1 if values['is_use_max'] else 0 if event == '生成': if not values['_FILE1_'] or not values['_FILE2_']: sg.popup_non_blocking('请正确提供参数') else: transclient(values['_FILE1_'], os.path.join(values['_FILE2_']), values['_LIBRARYNUM_'], is_use_balance, is_use_max) sg.Popup('排样成功!') window.Close() else: window.Close() break if __name__ == '__main__': make_gui()