wxpython实现图灵机器人的GUI-Python

#-*-coding:utf-8-*-
'''
Created on 2015年7月2日
@author: Administrator
'''
import wx
import urllib2
import sys
class login(wx.Frame):  
    def __init__(self, parent, id, title):  
        wx.Frame.__init__(self, parent, id, title,size=(800,600),style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX))  
        self.panel = wx.Panel(self, -1,size=(800,600))  
        self.panel.SetBackgroundColour('white') 
         
        box=wx.BoxSizer(wx.VERTICAL) 
        boxsizer1 = wx.BoxSizer(wx.HORIZONTAL)  
        boxsizer2 = wx.BoxSizer(wx.HORIZONTAL)  
        self.chat=wx.StaticText(self.panel,-1,u'聊天框:')
        self.chat_content=wx.TextCtrl(self.panel,-1,size=(735,500),style= wx.TE_READONLY|wx.TE_MULTILINE)
        self.yousay=wx.StaticText(self.panel,-1,u'回   复:')
        self.yousay_content=wx.TextCtrl(self.panel,-1,size=(650,45),style=wx.TE_PROCESS_ENTER)
        self.yousay_submit=wx.Button(self.panel,-1,u'发送')
        self.Bind(wx.EVT_BUTTON,self.submit,self.yousay_submit)
        self.Bind(wx.EVT_TEXT_ENTER, self.submit, self.yousay_content)
         
         
        boxsizer1.Add(self.chat,proportion=0,flag=wx.TOP,border=250)
        boxsizer1.Add(self.chat_content,proportion=0,flag=wx.EXPAND,border=0)
         
        boxsizer2.Add(self.yousay,proportion=0,flag=wx.TOP,border=10)
        boxsizer2.Add(self.yousay_content,proportion=0,flag=wx.EXPAND,border=0)
        boxsizer2.Add(self.yousay_submit,proportion=0,flag=wx.EXPAND,border=0)
         
        box.Add(boxsizer1,proportion=0,flag=wx.TOP|wx.LEFT,border=5)
        box.Add(boxsizer2,proportion=0,flag=wx.TOP|wx.LEFT,border=5)
        self.chat_content.AppendText(u"提示:你正在和图灵机器人聊天\n")
        self.SetSizer(box)
        self.Center()
        self.Show(True)
     
    def submit(self,event):
        yousay_content=self.yousay_content.GetValue()        
        url="http://www.tuling123.com/openapi/api?key=YOUR_KEY&info="
        if yousay_content == "bye":
            sys.exit()
         
        html=url+yousay_content.encode('utf-8')
        response=urllib2.urlopen(html,timeout=10)
        res=response.read()
        res=res.split('"')[5]
        res=res.decode("utf-8")       
         
        self.chat_content.AppendText(u"你说:%s" % yousay_content)
        self.chat_content.AppendText('\n')
        self.chat_content.AppendText(u'机器人:%s' % res)
        self.chat_content.AppendText('\n')
        self.yousay_content.Remove(0,self.yousay_content.GetLastPosition())
         
         
app = wx.App()  
login(None, -1, u'聊天机器人')  
app.MainLoop()


=====================================================

运行界面:


posted @ 2016-06-07 20:08  刘星湘  阅读(345)  评论(0编辑  收藏  举报