1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import tkinter import socket,sys import threading #创建主窗口 win = tkinter.Tk()
#设置标题 win.title("QQ客户端") #设置大小和位置 win.geometry("400x400+200+30")
ck = None
def getInfo(): while True: data = ck.recv(1024) text.insert(tkinter.INSERT,data.decode("utf-8"))
def connectServer(): global ck
ipStr = eip.get() portStr = eport.get() userStr = euser.get()
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ipStr, int(portStr))) client.send(userStr.encode("utf-8")) ck = client #等待接收数据 t= threading.Thread(target=getInfo) t.start() def sendMail(): friend = efriend.get() sendStr = esend.get() sendStr = friend + ":" + sendStr ck.send(sendStr.encode("utf-8"))
labelUser = tkinter.Label(win,text= "userName" ).grid(row = 0,column = 0) euser = tkinter.Variable() entryUser = tkinter.Entry(win, textvariable= euser ).grid(row = 0,column = 1)
labelIp = tkinter.Label(win,text= "ip" ).grid(row = 1,column = 0) eip = tkinter.Variable() entryIp = tkinter.Entry(win, textvariable= eip ).grid(row = 1,column = 1)
labelPort = tkinter.Label(win,text= "port" ).grid(row = 2,column = 0) eport = tkinter.Variable() entryPort = tkinter.Entry(win, textvariable= eport ).grid(row = 2,column = 1)
button1 = tkinter.Button(win, text ="连接", command=connectServer).grid(row = 3,column = 0)
#显示出来 text = tkinter.Text(win, width = 30, height = 5) text.grid(row = 4,column = 0)
esend = tkinter.Variable()
entrySend = tkinter.Entry(win, textvariable= esend,width =40,).grid(row = 6,column = 0) labelsend = tkinter.Label(win,text= "要发送的消息" ).grid(row = 7,column = 0) efriend = tkinter.Variable() entryFriend = tkinter.Entry(win, textvariable= efriend ).grid(row = 8,column = 0)
labelfriend = tkinter.Label(win,text= "好友名字" ).grid(row = 9,column = 0) button2 = tkinter.Button(win, text ="发送", command=sendMail).grid(row = 10,column = 0) win.mainloop()
|