本文仅在于实现界面,不讨论各种消息协议,具体消息协议后期有空再行更新,谢谢。

“”“
# 名 称:聊天界面
# 环 境:python 3.8.5(其他版本没测试,请自行测试)
# 模 块:tkinter/tkinter.messagebox/pickle/json
# 制 作:Q343340657(WX同号)
”“”

登录界面,效果如下:

代码如下:

“”“
# 名 称:聊天登录界面
# 环 境:python 3.8.5(其他版本没测试,请自行测试)
# 模 块:tkinter/tkinter.messagebox/pickle/json
# 制 作:Q343340657(WX同号)
”“”

import tkinter as tk
import tkinter.messagebox
import pickle
import json

# 窗口
login_win = tk.Tk()
login_win.title("聊天登录窗口")
sw = login_win.winfo_screenwidth()
sh = login_win.winfo_screenheight()
w = 690
h = 500
x = (sw - w) / 2
y = (sh - h) / 2
login_win.geometry("%dx%d+%d+%d" % (w, h, x, y))
login_win.resizable(0, 0)

# 图片,自行编辑,图片大小:690x300
login_benner = tk.PhotoImage(file='login_benner.png')
imgLabel = tk.Label(login_win, image=login_benner)
imgLabel.place(x=0, y=0)
# 标签 用户密码
tk.Label(login_win, text="用户名:").place(x=200, y=320)
tk.Label(login_win, text="密  码:").place(x=200, y=360)

# 文本框 用户名
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(login_win, textvariable=var_usr_name)
entry_usr_name.place(x=260, y=320)

# 文本框 密码
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(login_win, textvariable=var_usr_pwd, show="*")
entry_usr_pwd.place(x=260, y=360)


# 登录函数
def usr_login():
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    if usr_name == '111' and usr_pwd == '222':
        # 创建新的JSON
        new_usr = {
            'usrname': "'" + usr_name + "'",
            'age': '19'
        }
        with open('usr.json', 'w') as wp:
            json.dump(new_usr, wp)
        print(new_usr)
        login_win.destroy()
        import sx_friend_list #载入好友列表
    elif usr_name == '' or usr_pwd == '':
        tk.messagebox.showerror(message="用户名密码不能为空")
    else:
        tk.messagebox.showerror(message="用户名密码错误!")


# 登录
bt_login = tk.Button(login_win, text="登录(Login)", command=usr_login)
bt_login.place(x=202, y=400)
bt_quit = tk.Button(login_win, text="退出(Exit)")
bt_quit.place(x=350, y=400)

# 提示标签
tsLabel = tk.Label(login_win,
                   text="聊天登录界面 for Python Tkinter",
                   fg="red")
tsLabel.pack(side=tk.BOTTOM, expand='yes', anchor='se')
login_win.mainloop()

好友界面,效果如下:

代码如下:

#!/usr/bin/python
# encoding=utf8

“”“
# 名 称:聊天好友界面
# 环 境:python 3.8.5(其他版本没测试,请自行测试)
# 模 块:tkinter/tkinter.messagebox/pickle/json
# 制 作:Q343340657(WX同号)
”“”

import tkinter as tk
from tkinter import ttk
import pickle
import json

with open('usr.json', 'r') as fp:
    json_file = json.load(fp)
    json_str = json.dumps(json_file)
    json_date = json.loads(json_str)
    json_name = json_date['usrname']
    print(json_name)

fri_win = tk.Tk()
fri_win.title("[" + json_name + "] - 聊天界面 for Linux Bate")
# usrname = usr_name
# 聊天界面大小

w = 320
h = 800
sw = fri_win.winfo_screenwidth()
sh = fri_win.winfo_screenheight()
x = 200
y = (sh - h) / 2
fri_win.geometry("%dx%d+%d+%d" % (w, h, x, y))
fri_win.resizable(0, 0)

# 创建树形列表
fri_list = ttk.Treeview(fri_win, height=39, show="tree")
fri_list.place(x=10, y=30)

# 好友分组
# 1
fri_tree1 = fri_list.insert('', 0, 'frist', text='家人', values=("1"))
fri_tree1_1 = fri_list.insert(fri_tree1, 0, '001', text='老婆', values=("2"))
fri_tree1_2 = fri_list.insert(fri_tree1, 1, '002', text='女朋友001', values=("3"))
fri_tree2 = fri_list.insert('', 1, 'second', text='同事', values=("4"))
fri_tree2_1 = fri_list.insert(fri_tree2, 0, 'admin', text='女朋友002', values=("5"))
fri_tree2_2 = fri_list.insert(fri_tree2, 1, 'testadmin', text='女朋友003', values=("6"))

# 好友列表双击事件
def double_selected(event):
    for item in fri_list.selection():
        item_text = fri_list.item(item, "text")
        chat_usr = {
            'usrname': "'" + json_name + "'",
            'age': '19',
            'chatname': "'" + item_text + "'"
        }
        with open('usr.json', 'w') as wf:
            json.dump(chat_usr, wf)
        import sx_chat_win
        print(item_text)


fri_list.bind('<Double-1>', double_selected)
fri_list.pack(expand=True, fill=tk.X)

# 好友按钮
fri_btn = tk.Button(text="好友")
fri_btn.place(x=10, y=2)

# 群按钮
cla_btn = tk.Button(text="群聊")
cla_btn.place(x=70, y=2)

# 添加好友
into_fri_btn = tk.Button(text="添加好友")
into_fri_btn.place(x=130, y=2)

# 搜索好友
l1 = tk.Label(text="查找好友:")
l1.place(x=10, y=771)

# 搜索框
e1 = tk.Entry(width=12)
e1.place(x=80, y=770)

# 搜索按钮
search_btn = tk.Button(text="搜索")
search_btn.place(x=190, y=770)
# 菜单
menu_btn = tk.Button(text="设置")
menu_btn.place(x=256, y=770)

fri_win.mainloop()

聊天界面,效果如下:

代码如下:

#!/usr/bin/python
# encoding=utf8

“”“
# 名 称:聊天消息界面
# 环 境:python 3.8.5(其他版本没测试,请自行测试)
# 模 块:tkinter/tkinter.messagebox/pickle/json
# 制 作:Q343340657(WX同号)
”“”

import tkinter as tk
import time

# 发送消息
def sendMsg():
    t1_Msg.configure(state=tk.NORMAL)
    strMsg = "我:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\n'
    t1_Msg.insert("end", strMsg, 'green')
    sendMsg = t2_sendMsg.get('0.0', 'end')
    t1_Msg.insert("end", sendMsg)
    t2_sendMsg.delete('0.0', "end")
    t1_Msg.config(state=tk.DISABLED)
    print(strMsg + sendMsg)

# 创建窗口
app = tk.Tk()
app.title('与python聊天')

#
w = 800
h = 660
sw = app.winfo_screenwidth()
sh = app.winfo_screenheight()
x = 200
y = (sh - h) / 2
app.geometry("%dx%d+%d+%d" % (w, h, x, y))
app.resizable(0, 0)

# 远程按钮
desktop_btn = tk.Button(text="远程协助")
desktop_btn.place(x=700, y=2)

# 聊天消息预览窗口
t1_Msg = tk.Text(width=113, height=32)
t1_Msg.tag_config('green', foreground='#008C00')  # 创建tag
t1_Msg.place(x=2, y=35)
# t1_Msg.config(state=tk.DISABLED)
# t1_Msg.configure(state='disabled')

# 聊天消息发送
t2_sendMsg = tk.Text(width=112, height=10)
t2_sendMsg.place(x=2, y=485)

# 表情按钮
face_btn = tk.Button(text="表情")
face_btn.place(x=2, y=457)

# 文件按钮
face_btn = tk.Button(text="文件传送")
face_btn.place(x=62, y=457)

# 截图按钮
prtscr_btn = tk.Button(text="截图")
prtscr_btn.place(x=150, y=457)

# 聊天记录查询
chat_search = tk.Button(text="查找")
chat_search.place(x=210, y=457)

# 语音按钮
chat_search = tk.Button(text="语音")
chat_search.place(x=660, y=457)

# 视频按钮
chat_search = tk.Button(text="视频")
chat_search.place(x=720, y=457)

# 发送按钮
sendMsg_btn = tk.Button(text="发送(Send)", command=sendMsg)
sendMsg_btn.place(x=665, y=628)
# 主事件循环
app.mainloop()

 

更多推荐

Python 使用tkinter实现聊天窗口界面及简单的消息发送