Python 3 制作聊天机器人 教程

本教程是网上各处教程学来  自己拼接出来的 

本来是网上跟邹老师学Python的小练习 后来想自己加点对话 就自己写了个数组 随机回复 后来一想不是有聊天机器人么 

代码如下

# -*- coding: utf-8 -*-
"""
Created on Sat Jan  6 17:15:46 2018


@author: 
"""
import wx
import time
import random
import requests

class MyFrame(wx.Frame):
    msglist=["听不懂你在说什么","呵呵","What","WTF","要吃饭了","哈哈"] #自己写的随机的 用聊天机器人就没用了
    def __init__(self):#这是对话框
        wx.Frame.__init__(self,None,-1,"Chat",size=(520,450))
        panel=wx.Panel(self)
        labelAll=wx.StaticText(panel,-1,"All Contents")
        self.textAll=wx.TextCtrl(panel,
                                 -1,
                                 size=(480,200),
                                 style=wx.TE_MULTILINE|wx.TE_READONLY)
        labelIn=wx.StaticText(panel,-1,"I Say")
        self.textIn=wx.TextCtrl(panel,-1,size=(480,100),
                                 style=wx.TE_MULTILINE)
        
        self.btnSent=wx.Button(panel,-1,"Sent",size=(75,25))
        self.btnClear=wx.Button(panel,-1,"Clear",size=(75,25))
        self.Bind(wx.EVT_BUTTON,self.OnButtonSent,self.btnSent)
        self.Bind(wx.EVT_BUTTON,self.OnButtonClear,self.btnClear)
        
        btnSizer=wx.BoxSizer()
        btnSizer.Add(self.btnSent,proportion=0)
        btnSizer.Add(self.btnClear,proportion=0)
        
        mainSizer=wx.BoxSizer(wx.VERTICAL)
        
        mainSizer.Add(labelAll,proportion=0,flag=wx.ALIGN_CENTER)
        mainSizer.Add(self.textAll,proportion=1,flag=wx.EXPAND)
        
        mainSizer.Add(labelIn,proportion=0,flag=wx.ALIGN_CENTER)
        mainSizer.Add(self.textIn,proportion=0,flag=wx.EXPAND)
        mainSizer.Add(btnSizer,proportion=0,flag=wx.ALIGN_CENTER)
        panel.SetSizer(mainSizer)
        inmsg="萌萌哒的机器人 (%s):\n我是萌萌哒的机器人\n"%(time.ctime())
        self.textAll.AppendText(inmsg)
        
    def OnButtonSent(self,event):#发送按钮
       userinput=self.textIn.GetValue()
       self.textIn.Clear()
       now=time.ctime()
       inmsg="You (%s):\n%s\n"%(now,userinput)
       self.textAll.AppendText(inmsg)
       
       index=random.randint(0,len(self.msglist))
       print(index)
       if index==len(self.msglist):
           reply="你是说 %s 我还在学说话"%(userinput)
       else:
           reply=get_response(userinput)
       print(reply)
       replymsg="萌萌哒的机器人 (%s):\n%s\n"%(now,reply)
       self.textAll.AppendText(replymsg)
       #self.textAll.SetValue(userinput)
       
    def OnButtonClear(self,event): #清除按钮
        self.textAll.Clear()
        
KEY = 'ca098ebe818b49df98af997bef29b3b3' #这个key可以直接拿来用 用的是萌新的学习日记 的账号 我懒的申请了 因为只是测测功能
def get_response(msg):
    Url='http://www.tuling123.com/openapi/api'
    data={'key':KEY,
          'info':msg,
          'userid':'pth-robot',
         }
    try:
        r=requests.post(Url,data=data).json()
        return r.get('text')
    except:
        return
app=wx.App()   #实例化App
frame=MyFrame()
frame.Show()
app.MainLoop()   
     


用的anaconda开发 如果Spyder只能运行一次 出现PyNoAppError: The wx.App object must be created first! 那么右击选Quit退出就可以再次运行了

打包需要切换到 py所在目录下后 命令里直接运行 pyinstaller -F -w ***.py    (需安装pyinstaller) 然后你在dist文件夹下就能找到exe了


猜你喜欢

转载自blog.csdn.net/wsxhking/article/details/78991876