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()


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

运行界面:


转载于:https://www.cnblogs.com/liuxingxiang/p/5572647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值