python对话框大小_python——简易聊天对话框

效果图:

db576a9777f28528ee7365629901a0ac.png

客户端代码:

import tkinter as tk

from tkinter import scrolledtext

import socket

import threading

from datetime import datetime

def tcp_recv(sock):

while True:

str = sock.recv(1024).decode("utf-8")

show_info(str)

def send_func(sock):

str = send_msg.get("0.0", "end")

sock.send(str.encode("utf-8"))

show_info(str)

def show_info(str):

now = datetime.now()

s_time = now.strftime("%Y-%m-%d %H:%M:%S")

str = str.rstrip()

if len(str) == 0:

return -1

send_msg.delete("0.0", "end")

temp = s_time + "\n " + str + "\n"

show_msg.insert(tk.INSERT, "%s" % temp)

msFont = '微软雅黑' #字体

fontSize = 18 #字体大小

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

sock.connect(("127.0.0.1",8888))

mainWindow = tk.Tk()

mainWindow.title("客户端")

mainWindow.minsize(400,400)

show_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))

show_msg.place(width=400,height=250,x=0,y=0)

#show_msg.insert(tk.INSERT,"%s 已连接\n"%addr[0])

send_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))

send_msg.place(width=400,height=140,x=0,y=260)

button_send = tk.Button( mainWindow, font=(msFont,fontSize),text = "发 送",bg="orange",fg="white",

command=lambda:send_func(sock))

button_send.place(width=100,height=40,x=300,y=360)

t = threading.Thread(target=tcp_recv,args=(sock,))

t.start()

tk.mainloop()

服务器代码:

import tkinter as tk

from tkinter import scrolledtext

import socket

import threading

from datetime import datetime

def tcp_recv(sock):

while True:

str = sock.recv(1024).decode("utf-8")

show_info(str)

def send_func(sock):

str = send_msg.get("0.0", "end")

sock.send(str.encode("utf-8"))

show_info(str)

def show_info(str):

now = datetime.now()

s_time = now.strftime("%Y-%m-%d %H:%M:%S")

str = str.rstrip()

if len(str) == 0:

return -1

send_msg.delete("0.0", "end")

temp = s_time + "\n " + str + "\n"

show_msg.insert(tk.INSERT, "%s" % temp)

msFont = '微软雅黑' #字体

fontSize = 18 #字体大小

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

sock.bind(("127.0.0.1",8888))

sock.listen(5)

mainWindow = tk.Tk()

mainWindow.title("服务器")

mainWindow.minsize(400,400)

show_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))

show_msg.place(width=400,height=250,x=0,y=0)

send_msg = scrolledtext.ScrolledText(mainWindow,font=(msFont,fontSize))

send_msg.place(width=400,height=140,x=0,y=260)

button_send = tk.Button( mainWindow, font=(msFont,fontSize),text = "发 送",bg="orange",fg="white",

command=lambda:send_func(s))

button_send.place(width=100,height=40,x=300,y=360)

s,addr = sock.accept()

t = threading.Thread(target=tcp_recv,args=(s,))

t.start()

tk.mainloop()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值