layout/T7_client.py

110 lines
3.7 KiB
Python
Raw Normal View History

2023-12-07 17:47:53 +08:00
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)
2024-02-05 17:40:29 +08:00
def senddata(conn, path, librarynum, is_use_balance, is_use_max):
2023-12-07 17:47:53 +08:00
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,
2024-02-05 17:40:29 +08:00
librarynum=librarynum,
is_use_balance=is_use_balance,
is_use_max=is_use_max
2023-12-07 17:47:53 +08:00
)
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)
2024-02-05 17:40:29 +08:00
dest_ip = '192.168.38.90'
2023-12-07 17:47:53 +08:00
dest_port = int(8191)
client.connect((dest_ip, dest_port))
return client
2024-02-05 17:40:29 +08:00
def transclient(sendfile, resfile, librarynum, is_use_balance, is_use_max):
2023-12-07 17:47:53 +08:00
conn = connect()
2024-02-05 17:40:29 +08:00
senddata(conn, sendfile, librarynum, is_use_balance, is_use_max)
2023-12-07 17:47:53 +08:00
recvdata(conn, resfile)
def make_gui():
sg.theme('DarkBlack1')
layout = [
2024-01-02 13:53:43 +08:00
[sg.Text('排样管数'), sg.Spin([i for i in range(150)], initial_value=130, size=(3, 1), key='_LIBRARYNUM_')],
2023-12-07 17:47:53 +08:00
[sg.Text()],
2024-02-05 17:40:29 +08:00
[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()],
2023-12-07 17:47:53 +08:00
[
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('取消')]]
2024-02-05 17:40:29 +08:00
# iconpath = os.path.join(os.path.abspath(sys.path[0]), 'other', 'icon.ico')
2024-01-02 13:53:43 +08:00
window = sg.Window('解码排样T7程序', layout, font='Helvetica 11', icon=r'D:\project\autulayout\other\icon.ico')
2023-12-07 17:47:53 +08:00
while True:
2024-01-02 13:53:43 +08:00
event, values = window.read()
2024-02-05 17:40:29 +08:00
is_use_balance = 1 if values['is_use_balance'] else 0
is_use_max = 1 if values['is_use_max'] else 0
2023-12-07 17:47:53 +08:00
if event == '生成':
if not values['_FILE1_'] or not values['_FILE2_']:
sg.popup_non_blocking('请正确提供参数')
else:
2024-02-05 17:40:29 +08:00
transclient(values['_FILE1_'], os.path.join(values['_FILE2_']), values['_LIBRARYNUM_'],
is_use_balance, is_use_max)
2023-12-07 17:47:53 +08:00
sg.Popup('排样成功!')
window.Close()
else:
window.Close()
break
if __name__ == '__main__':
make_gui()