【python】送给女神的AI聊天软件(内附源码)

想起了前段时间520给女朋友写的自动聊天软件,现在分享一下吧。

目录

先看一下效果图

需要用到的库: tkinter、time、urllib、requests

窗体设计:

 函数:

为粉丝们额外添加的功能:根据女友输入的内容自定义回复

添加后的效果图:

完整代码:


先看一下效果图

 

就当是女友无聊的时候自己抽不出时间的小分身吧!

需要用到的库: tkinter、time、urllib、requests

tkinter负责窗体、time显示时间、urllib和requests负责请求

窗体设计:

from tkinter import *
win1 = Tk()
win1.geometry('400x644+100+100')
win1.title('xxx男神的AI分身')
Label11 = Label(win1, text='男神白', font=('黑体', 12), anchor='center').place(y=13, x=15, width=380, height=20)
Entry11 = Entry(win1, font=('等线', 11), width = 70)
Entry11.place(y=600, x=15, width=310, height=26)
Button11 = Button(win1, text='发送', font=('等线', 11), command = mecha).place(y=598, x=328, width=65, height=30)
console = Text(win1, font=('等线', 11))
console.place(y=35, x=15, width=368, height=550)
console.insert(1.0,'             欢迎来到你与男神的小天地!\n你可以把你想说的内容输入到下面的输入框哦\n')
console.mark_set('markOne', 1.0)
console.mark_set('markTwo', 3.0)
console.tag_add('tag1', 'markOne', 'markTwo')
console.tag_config('tag1', foreground='red')
win1.bind("<Return>", test_fun)
win1.mainloop()

 函数:

在txet部件上显示发送时间及颜色处理,使用requests和urllib库来调用接口处理回复你女朋友的信息。

def liaotian():
    global b
    import time
    b = 3
    def mecha():
        global b
        b+=2
        console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
        console.insert('end',str(Entry11.get())+'\n')
        console.mark_set(str(b-1), str(b-1)+'.0')
        console.mark_set(str(b), str(b)+'.0')
        console.tag_add(str(b), str(b-1), str(b))
        console.tag_config(str(b), foreground='blue')
        console.see(END)
        console.update()
        console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
        console.insert('end',aicha()+'\n')
        console.mark_set(str(b-1), str(b-1)+'.0')
        console.mark_set(str(b), str(b)+'.0')
        console.tag_add(str(b), str(b-1), str(b))
        console.tag_config(str(b), foreground='red')
        console.see(END)
        Entry11.delete(0,END)
        console.update()
    def test_fun(self):
        mecha()
    def aicha():
        global b
        b+=2
        msg = str(Entry11.get())

        else:
            import urllib
            import requests
            def qingyunke(msg):
                url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
                html = requests.get(url)
                return html.json()["content"]
            print("原话>>", msg)
            res = qingyunke(msg)
            res = res.replace('菲菲', '你男神')
            res = res.replace('我', '你男神')
            print("智能回复>>", res)
            return res

为粉丝们额外添加的功能:根据女友输入的内容自定义回复

我自己添加的内容太肉麻的,不太适合分享给你们哈。于是我就给你们做多了一个自定义回复的功能。嘿嘿!那就是独一无二的代码了

坚持着你们直接能使用代码不做任何修改的原则,我就不让你们在代码里面添加了,当你第一次运行此代码的时候会自动创建一个txt文件(甚至还不用让你创建文件),你就可以在txt文件中自定义回复内容了。

使用异常处理模块try来尝试open读取名为“自定义回复.txt”的文件,若不存在except就创建,若已存在直接读取即可。操作读取的字符串逐个添加到zidingyi字典中,判断输入的语句是否在zidingyi.keys()中即可做出相应回复。

添加后的效果图:

    zidingyi = {}
    try:
        with open("自定义回复.txt", "r", encoding='utf8') as f:
            asd = f.readlines()
            print(asd)
            for line in asd:
                line = line.strip('\n')
                wen, da = line.split(':', 1)
                zidingyi[wen] = da
    except:
        with open("自定义回复.txt", "w+", encoding='utf8') as f:
            f.write('提示——>采用“输入:回复”格式   如——>你吃饭了吗?:我吃饭啦           回车以继续下一自定义回复(注意使用英文的冒号)')
        with open("自定义回复.txt", "r", encoding='utf8') as f:
            asd = f.readlines()
            print(asd)
            for line in asd[1:]:
                line = line.strip('\n')
                wen, da = line.split(':', 1)
                zidingyi[wen] = da
                print(line)
    print(zidingyi)

完整代码:

from tkinter import *


def liaotian():
    global b
    import time
    b = 3

    def mecha():
        global b
        b += 2
        console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
        console.insert('end', str(Entry11.get()) + '\n')
        console.mark_set(str(b - 1), str(b - 1) + '.0')
        console.mark_set(str(b), str(b) + '.0')
        console.tag_add(str(b), str(b - 1), str(b))
        console.tag_config(str(b), foreground='blue')
        console.see(END)
        console.update()
        console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
        console.insert('end', aicha() + '\n')
        console.mark_set(str(b - 1), str(b - 1) + '.0')
        console.mark_set(str(b), str(b) + '.0')
        console.tag_add(str(b), str(b - 1), str(b))
        console.tag_config(str(b), foreground='red')
        console.see(END)
        Entry11.delete(0, END)
        console.update()

    def test_fun(self):
        mecha()

    def aicha():
        global b
        b += 2
        msg = str(Entry11.get())
        if msg in zidingyi.keys():
            res = zidingyi[msg]
            return res
        else:
            import urllib
            import requests
            def qingyunke(msg):
                url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
                html = requests.get(url)
                return html.json()["content"]

            print("原话>>", msg)
            res = qingyunke(msg)
            res = res.replace('菲菲', '你男神')
            res = res.replace('我', '你男神')
            print("智能回复>>", res)
            return res

    zidingyi = {}
    try:
        with open("自定义回复.txt", "r", encoding='utf8') as f:
            asd = f.readlines()
            print(asd)
            for line in asd:
                line = line.strip('\n')
                wen, da = line.split(':', 1)
                zidingyi[wen] = da
    except:
        with open("自定义回复.txt", "w+", encoding='utf8') as f:
            f.write('提示——>采用“输入:回复”格式   如——>你吃饭了吗?:我吃饭啦           回车以继续下一自定义回复(注意使用英文的冒号)')
        with open("自定义回复.txt", "r", encoding='utf8') as f:
            asd = f.readlines()
            print(asd)
            for line in asd[1:]:
                line = line.strip('\n')
                wen, da = line.split(':', 1)
                zidingyi[wen] = da
                print(line)
    print(zidingyi)
    win1 = Tk()
    win1.geometry('400x644+100+100')
    win1.title('男神的AI分身')
    Label11 = Label(win1, text='你男神', font=('黑体', 12), anchor='center').place(y=13, x=15, width=380, height=20)
    Entry11 = Entry(win1, font=('等线', 11), width=70)
    Entry11.place(y=600, x=15, width=310, height=26)
    Button11 = Button(win1, text='发送', font=('等线', 11), command=mecha).place(y=598, x=328, width=65, height=30)
    console = Text(win1, font=('等线', 11))
    console.place(y=35, x=15, width=368, height=550)
    console.insert(1.0, '                   欢迎来到你与男神的小天地!\n      你可以把你想说的内容输入到下面的输入框哦\n')
    console.mark_set('markOne', 1.0)
    console.mark_set('markTwo', 3.0)
    console.tag_add('tag1', 'markOne', 'markTwo')
    console.tag_config('tag1', foreground='red')
    win1.bind("<Return>", test_fun)
    win1.mainloop()


liaotian()

怎么样,是不是特别简单~快复制去送给你心中的那个女神吧~

如果有帮助到你的,麻烦点赞收藏关注一下,后面会不定期更新自己写的一些有趣的python代码

再次感谢那些不离不弃的铁粉,我们下篇见~

  • 12
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 聊天源码是一个使用 Python 编程语言编写的聊天室应用程序的源代码。这个源码可以用于构建一个简单的聊天室,使用户可以在不同的终端之间进行实时的文本交流。 这个聊天源码通常包含以下几个主要部分: 1. 服务器端代码:包含创建服务器套接字、监听连接请求、接收和发送数据等功能的代码。服务器端负责接收和处理来自客户端的连接请求,并将消息广播给其他已连接的客户端。 2. 客户端代码:包含创建客户端套接字、连接到服务器、发送和接收数据等功能的代码。客户端负责与服务器建立连接,发送消息给服务器,并接收来自服务器的广播消息。 3. 用户界面代码:包含用户界面的设计和实现,以便用户可以方便地输入和查看消息。这部分通常使用图形用户界面(GUI)库或命令行界面(CLI)库进行实现。 聊天源码通常还会包含一些辅助功能,例如用户注册、登录、私聊等等。这些功能可以根据需求进行扩展和修改。 Python 聊天源码可以通过互联网上的开源项目、教程或者自行编写来获得。使用这些源码可以帮助开发者了解和学习网络编程、套接字操作以及多线程编程的基本知识和技术。 在实际应用中,如果要将聊天源码投入生产环境,可能需要进行一些安全性和性能方面的优化。这包括对代码进行安全审计,处理并发连接的同步问题,以及对不同操作系统和网络环境的适配等等。 总之,Python 聊天源码提供了一个基础框架,可以帮助开发者构建自己的聊天室应用程序,以满足实时通信的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值