当前位置: 首页 > article >正文

UI Left EXE Right Recive TCPIP

import tkinter as tk
from tkinter import messagebox, filedialog
from tkinter import ttk
import socket
import threading
import subprocess
import time
from datetime import datetime
from PIL import Image, ImageTk

# 全局变量
client_socket = None
connected = False

def connect_to_server():
    global client_socket, connected
    ip = "127.0.0.1"
    port = 7930
    
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect((ip, port))
        connected = True
        status_indicator.config(bg='green')
        messagebox.showinfo("Success", f"Connected to server at {ip}:{port}")
        
        # Start a new thread to receive data
        threading.Thread(target=receive_data).start()
    except Exception as e:
        connected = False
        status_indicator.config(bg='red')
        messagebox.showerror("Error", f"Failed to connect to server: {e}")

def disconnect_from_server():
    global client_socket, connected
    if connected:
        client_socket.close()
        connected = False
        status_indicator.config(bg='red')
        messagebox.showinfo("Disconnected", "Disconnected from server")

def receive_data():
    while connected:
        try:
            data = client_socket.recv(1024).decode('utf-8')
            if not data:
                break
            current_time = datetime.now().strftime("%H:%M:%S")
            received_data_text.insert(tk.END, f"[{current_time}] {data}\n")
            received_data_text.yview_moveto(1)  # Scroll to the bottom
        except Exception as e:
            print(f"Error receiving data: {e}")
            break
    disconnect_from_server()

def launch_application():
    app_path = r"D:\aa\Public_Release\aa.exe"
    try:
        subprocess.Popen(app_path)
    except Exception as e:
        messagebox.showerror("Error", f"Failed to launch application: {e}")

# 创建主窗口
root = tk.Tk()
root.title("TCP Client")
root.geometry("800x600")

# 连接状态指示器(右上角)
status_indicator = tk.Frame(root, width=100, height=100, bg='red')
status_indicator.place(x=700, y=0)

# 左侧区域 - 启动应用程序按钮
left_frame = tk.Frame(root, width=800, height=300)
left_frame.pack(side=tk.TOP, fill=tk.X)

# 加载并设置图像
image_path = r"C:\Users\Administrator\Desktop\Study\i.png"  # 替换为实际图像路径
try:
    image = Image.open(image_path)
    image = image.resize((150, 150), Image.Resampling.LANCZOS)
    img = ImageTk.PhotoImage(image)
except IOError:
    img = None

launch_button = tk.Button(left_frame, image=img, command=launch_application)
if img:
    launch_button.image = img  # Keep a reference!
launch_button.pack(pady=125)

# 右侧区域 - 显示接收到的数据
right_frame = tk.Frame(root, width=800, height=300)
right_frame.pack(side=tk.TOP, fill=tk.X)

received_data_label = tk.Label(right_frame, text="Received Data:")
received_data_label.pack(pady=10)

received_data_text = tk.Text(right_frame, wrap=tk.WORD, height=15, width=60)
received_data_text.pack(padx=10, pady=10)

# 控制按钮
control_frame = tk.Frame(root)
control_frame.pack(pady=10)

connect_button = tk.Button(control_frame, text="Connect", command=connect_to_server)
connect_button.grid(row=0, column=0, padx=5)

disconnect_button = tk.Button(control_frame, text="Disconnect", command=disconnect_from_server)
disconnect_button.grid(row=0, column=1, padx=5)

# 初始化全局变量
client_socket = None
connected = False

# 运行主循环
root.mainloop()


http://www.kler.cn/a/428199.html

相关文章:

  • [JavaWeb]搜索表单区域
  • VLLM性能调优
  • 【PyTorch】6.张量形状操作:在深度学习的 “魔方” 里,玩转张量形状
  • 使用Ollama本地部署DeepSeek R1
  • C++,STL 简介:历史、组成、优势
  • C++并发编程指南02
  • YOLOv8-ultralytics-8.2.103部分代码阅读笔记-build.py
  • 【如何制定虚拟货币的补仓策略并计算回本和盈利】
  • Linux网络编程之---组播和广播
  • 快速排序的基本思想和java实现
  • Next.js系统性教学:全面掌握客服务端组件(Server Components)
  • ARMv8-A MacOS调试环境搭建
  • Python毕业设计选题:基于大数据的淘宝电子产品数据分析的设计与实现-django+spark+spider
  • PyCharm文件、临时文件、目录、文件夹(Directory)、软件包(Package)的区别
  • Spring Boot配置文件敏感信息加密
  • 智创 AI 新视界 -- AI 与量子计算的未来融合前景(16 - 5)
  • python拆分Excel文件
  • docker安装ddns-go(外网连接局域网)
  • JVM 参数配置详细介绍
  • C++ Learning 函数重载•引用
  • PyTorch基本使用——张量的索引操作
  • opencv光流法推测物体的运动
  • Spring Boot日志:从Logger到@Slf4j的探秘
  • ChatGPT 最新推出的 Pro 订阅计划,具备哪些能力 ?
  • uniapp 微信小程序webview 和 h5数据通信
  • 【AWS re:Invent 2024】一文了解EKS新功能:Amazon EKS Auto Mode