回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

通过python实现人工智能(智障) 简单

头像 DFWLY 2021.07.18 277 0

这一个项目主要通过python的图形化库tkinter实现,人机对话通过青云客提供的API接口实现-->青云客智能聊天机器人API (qingyunke.com)

下面我们直接上代码:

(实现效果)

project-image
project-image
代码
'''
按send后又延时,请注意
'''
import tkinter as tk
import time
import urllib.request
import urllib.parse
import requests


def Text():
    time_me = ('我:' + time.strftime('%Y-%m-%d %H:%M:%S') + '\n')
    time_Emma = ('Emma:' + time.strftime('%Y-%m-%d %H:%M:%S') + '\n')  # , time.localtime()
    ask = msg.get()
    msg.delete(0, 'end')
    disp.insert("end", time_me)  # 获取发送消息,添加文本到消息列表
    disp.insert("end", ask + "\n")
    url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(ask))
    html = requests.get(url)
    res = html.json()["content"]
    disp.insert("end", time_Emma)
    disp.insert("end", res + "\n\n---------------->>\n")
    rec = disp.get("0.0", "end")
    

def Event(event):  # 绑定enter键
    if event.keysym == 'Return':
        Text()


win = tk.Tk()
win.title('Emma-AI')
win.geometry('600x371')
win.iconbitmap('fa.ico') # 请将图标放于程序同目录下
tk.Label(win, text='消息记录框:').pack()
disp = tk.Text(win, width=600, height=20)
disp.pack()  # 文本输入框
tk.Label(win, text='消息输入框:').pack()
msg = tk.Entry(win, width=600)  # 内容输入框
msg.pack()
msg.bind('<KeyPress-Return>', Event)
# 发送
btnRead = tk.Button(win, height=1, width=10, text="Send", command=Text).pack()  

win.mainloop()

评论

user-avatar