首先是项目需要的包importurllib.requestimporturllib.parsefromtkinterimport*importtime函数部分说明:调用服务端接口实现非特定智能回复defget_robot_replay(question):'''函数功能:给具体问题具体回答,其他非具体问题,智能回复参数说明:问题:聊天内容或问题返回值:str,回复内容'''if"what'syourname"inquestion:answer="IamYouyou"elif"youHowoldareyou"inquestion:answer="18"elif"AreyouGGorMM"inquestion:answer="MM"else:try:#调用NLP接口实现智能回复params=urllib.parse.urlencode({'msg':question}).encode()#将str转为byte类型,参数接口需要进行URL编码req=urllib.request.Request("http://api.itmojun.com/chat_robot",params,method="POST")#创建请求对象answer=urllib.request.urlopen(req).read().decode()#调用接口(sendHTTP请求到目标服务器)exceptExceptionase:answer="AIrobotisoutoforder!(Reason:%s)"%ereturnanswer回复格式和界面设计defmsgsend():msg='I'+time.strftime('%Y-%m-%d%H:%M:%S',time.localtime())+'\n'txt_msglist.insert(END,msg,'green')#添加时间txt_msglist.insert(END,txt_msgsend.get('0.0',END))#获取发送的消息,在消息列表中添加文字msg1='优优大宝贝'+time.strftime('%Y-%m-%d%H:%M:%S',time.localtime())+'\n'txt_msglist.insert(END,msg1,'green')#添加时间txt_msglist.insert(END,get_robot_replay(txt_msgsend.get('0.0',END)))txt_msgsend.delete('0.0',END)#清除发送消息defcancel():txt_msgsend.delete('0.0',END)#取消发送消息,即清除发送消息'''并绑定向上键'''defmsgsendEvent(event):ifevent.keysym=='Up':msgsend()聊天界面设计(原文源码)原文地址:https://developer.aliyun.com/…
