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

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

“”“
# 名 称:聊天界面
# 环 境: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()

 

好的,以下是使用tkinter实现计算器界面的Python代码: ```python import tkinter as tk class Calculator: def __init__(self, master): self.master = master master.title("Calculator") # 创建显示屏 self.screen = tk.Entry(master, width=25, font=('Arial', 16)) self.screen.grid(row=0, column=0, columnspan=4, padx=5, pady=5) # 创建按钮 self.create_button("7", 1, 0) self.create_button("8", 1, 1) self.create_button("9", 1, 2) self.create_button("/", 1, 3) self.create_button("4", 2, 0) self.create_button("5", 2, 1) self.create_button("6", 2, 2) self.create_button("*", 2, 3) self.create_button("1", 3, 0) self.create_button("2", 3, 1) self.create_button("3", 3, 2) self.create_button("-", 3, 3) self.create_button("0", 4, 0) self.create_button(".", 4, 1) self.create_button("C", 4, 2) self.create_button("+", 4, 3) self.create_button("=", 5, 0, columnspan=4) def create_button(self, text, row, column, columnspan=1): button = tk.Button(self.master, text=text, width=5, height=2, font=('Arial', 16), command=lambda: self.button_click(text)) button.grid(row=row, column=column, columnspan=columnspan, padx=5, pady=5) def button_click(self, text): if text == "=": try: result = eval(self.screen.get()) self.screen.delete(0, tk.END) self.screen.insert(0, str(result)) except: self.screen.delete(0, tk.END) self.screen.insert(0, "Error") elif text == "C": self.screen.delete(0, tk.END) else: self.screen.insert(tk.END, text) root = tk.Tk() calculator = Calculator(root) root.mainloop() ``` 以上代码创建了一个名为Calculator的类,其中包含了创建显示屏和按钮的方法。在button_click方法中,我们使用eval函数计算表达式的结果,并将结果显示在显示屏上。如果表达式无法计算,我们将显示“Error”消息。 我们在主函数中创建一个Tkinter窗口并实例化Calculator类。最后,我们通过调用主循环方法让程序进入事件循环,开始监听并响应用户输入。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个爱折腾的小人物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值