Java教程

客户端

本文主要是介绍客户端,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from tkinter import *
import socket
import threading
from tkinter.filedialog import askopenfilenames


class Client1:
    def __init__(self):
        self.start1()
    def __del__(self):
        self.client1.close()
    def start1(self):
        self.init1 = Tk()
        self.init1.title("客户端聊天工具")
        self.init1.geometry('500x600')
        self.init1["bg"] = 'pink'
        self.init1.attributes("-alpha", 0.8)
        self.text3 = Text(self.init1, width=40, height=2)
        self.text3.place(x=20, y=330)
        self.text3.insert("end", "192.168.0.103")
        self.bu1 = Button(self.init1, text="连接",command=self.link)
        self.bu1.place(x=330, y=330)
        self.text1 = Text(self.init1, width=60, height=20)
        self.text1.place(x=20, y=50)
        self.w = Label(self.init1, text="客户端信息框", width=60)
        self.w.place(x=20, y=20)
        self.bu = Button(self.init1, text="发送",command=self.add1)
        self.bu.place(x=20, y=430)
        self.text2 = Text(self.init1, width=25, height=3)
        self.text2.place(x=80, y=430)
        self.init1.mainloop()

    def link(self):
        temp = self.text3.get(1.0, END)
        temp = temp[:-1]
        print(len(temp))
        self.client1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client1.connect((temp, 8080))
        self.text1.insert("end", f"连接{temp}成功!" + "\n"+"\n")
        self.init1.after(1000, self.get2)

    def upload(self):
        askopenfilenames()




    def get2(self):
        t = threading.Thread(target=self.get1)

        #t.setDaemon(True)

        t.start()

        self.init1.after(1000, self.get2)

    def get1(self):
        msg = self.client1.recv(1024)

        if msg is not None:
            msg = msg.decode('utf-8')

        self.text1.insert("end", "服务端发来的消息:" + msg + "\n")

    def add1(self):
        temp = self.text2.get(1.0, "end")

        temp1 = temp

        self.client1.send(temp1.encode('utf-8'))

        self.text1.insert("end", "你发送的消息:" + temp + "\n")

        self.text2.delete(1.0, "end")



a = Client1()

 

这篇关于客户端的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!