語言:Python
工具:MySQL,Tkinter,圖靈機器人
功能:圖形聊天工具,可以選擇自動回復或者人工回復。
注意:如果運行需要自建mysql數據庫表、還有安裝各種模塊、還有到“圖靈機器人”申請賬戶(免費),否則看看就行了。
(部分借鑒自某個博客,忘記哪里看的了,tkinter部分和tcp連接都是從那里學來的)
1、服務器
#filename:Server+SwitchButton.py#-*-coding:utf-8-*-import Tkinterimport tkFontimport socketimport threadimport timeimport urllibimport urllib2import tracebackimport MySQLdbimport randomimport sysreload(sys)sys.setdefaultencoding('utf8')class Server(): title = 'Python自動客服-服務器' ip = '127.0.0.1'#還不知怎么用私網連接,目前只能用公網ip設置serverIp Port = 8808#端口必須在1024以上 global serverSock global receiveMsg global message flag = False #初始化類的相關屬性,類似于構造方法 def __init__(self): from Tkinter import * self.root = Tkinter.Tk() self.root.title(self.title) #窗口面板,用6個frame面板布局,ip和port設置框架最后因為連接方式的選取問題取消 self.frame = [Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame(), Tkinter.Frame()] #frame0 #切換功能的checkButton self.CheckVar1 = IntVar() self.CheckVar2 = IntVar() self.CheckVar3 = IntVar() self.CheckButton1 = Checkbutton(self.frame[0], text = "手動", variable = self.CheckVar1, onvalue = 1, offvalue = 0, height=2, width = 5) self.CheckButton2 = Checkbutton(self.frame[0], text = "數據庫", variable = self.CheckVar2, onvalue = 1, offvalue = 0, height=2, width = 5) self.CheckButton3 = Checkbutton(self.frame[0], text = "圖靈", variable = self.CheckVar3, onvalue = 1, offvalue = 0, height=2, width = 5) self.CheckButton1.pack(expand=1, side=Tkinter.TOP and Tkinter.LEFT) self.CheckButton2.pack(expand=1, side=Tkinter.TOP and Tkinter.LEFT) self.CheckButton3.pack(expand=1, side=Tkinter.TOP and Tkinter.RIGHT) self.frame[0].pack(fill=Tkinter.BOTH)#expand=1, ''' #frame1 #ip地址和端口選擇,暫時取消 ft1 = tkFont.Font(family='Fixdsys', size=8) self.inputTextIp = Tkinter.Text(self.frame[1], width=20, height=2, font=ft1) self.inputTextPort = Tkinter.Text(self.frame[1], width=7, height=2, font=ft1) self.ipConfirmButton = Tkinter.Button(self.frame[1], text='確認', width=5, height=2, command=self.setIpPort) self.inputTextIp.pack(expand=1, fill=Tkinter.BOTH) self.inputTextPort.pack(expand=1, fill=Tkinter.BOTH) self.frame[1].pack(expand=1, fill=Tkinter.BOTH) ''' #frame2 #顯示消息Text右邊的滾動條 self.chatTextScrollBar = Tkinter.Scrollbar(self.frame[2]) self.chatTextScrollBar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) #顯示消息Text,并綁定上面的滾動條 #本來應該相互綁定,但實際運行時,滑動條沒有綁定列表框 self.chatText = Tkinter.Listbox(self.frame[2], width=80, height=18) self.chatText['yscrollcommand'] = self.chatTextScrollBar.set self.chatText.pack(expand=1, side=Tkinter.LEFT, fill=Tkinter.BOTH) #self.chatTextScrollBar['command'] = self.chatText.yview() self.frame[2].pack(fill=Tkinter.BOTH) #frame3 #標簽,創建高度為2的空白區區分ListBox和Text label = Tkinter.Label(self.frame[3], height=2) label.pack(fill=Tkinter.BOTH) self.frame[3].pack(fill=Tkinter.BOTH) #frame4 #輸入消息Text的滾動條 self.inputTextScrollBar = Tkinter.Scrollbar(self.frame[4]) self.inputTextScrollBar.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) #輸入消息Text,并與滾動條綁定 #常用
2、客戶端
#filename:TuringTkClient.py#-*-coding:utf-8-*-# Python在線聊天客戶端import Tkinterimport tkFontimport socketimport threadimport timeimport sysreload(sys);sys.setdefaultencoding('utf8');class ClientUI(): title = 'Python自動客服-客戶端' local = '127.0.0.1' port = 8808 global clientSock; flag = False #初始化類的相關屬性,類似于構造方法 def __init__(self): self.root = Tkinter.Tk() self.root.title(self.title) #窗口面板,用4個面板布局 self.frame = [Tkinter.Frame(),Tkinter.Frame(),Tkinter.Frame(),Tkinter.Frame()] #顯示消息Text右邊的滾動條 self.chatTextScrollBar = Tkinter.Scrollbar(self.frame[0]) self.chatTextScrollBar.pack(side=Tkinter.RIGHT,fill=Tkinter.Y) #顯示消息Text,并綁定上面的滾動條 self.chatText = Tkinter.Listbox(self.frame[0],width=80,height=18) self.chatText['yscrollcommand'] = self.chatTextScrollBar.set self.chatText.pack(expand=1,fill=Tkinter.BOTH) self.chatTextScrollBar['command'] = self.chatText.yview() self.frame[0].pack(expand=1,fill=Tkinter.BOTH) #標簽,分開消息顯示Text和消息輸入Text label = Tkinter.Label(self.frame[1],height=2) label.pack(fill=Tkinter.BOTH) self.frame[1].pack(expand=1,fill=Tkinter.BOTH) #輸入消息Text的滾動條 self.inputTextScrollBar = Tkinter.Scrollbar(self.frame[2]) self.inputTextScrollBar.pack(side=Tkinter.RIGHT,fill=Tkinter.Y) #輸入消息Text,并與滾動條綁定 ft = tkFont.Font(family='Fixdsys',size=11) self.inputText = Tkinter.Text(self.frame[2],width=80,height=8,font=ft) self.inputText['yscrollcommand'] = self.inputTextScrollBar.set self.inputText.pack(expand=1,fill=Tkinter.BOTH) self.inputTextScrollBar['command'] = self.chatText.yview() self.frame[2].pack(expand=1,fill=Tkinter.BOTH) #發送消息按鈕 self.sendButton=Tkinter.Button(self.frame[3],text=' 發 送 ',width=10,command=self.sendMessage) self.sendButton.pack(expand=1,side=Tkinter.BOTTOM and Tkinter.RIGHT,padx=15,pady=8) #關閉按鈕 self.closeButton=Tkinter.Button(self.frame[3],text=' 關 閉 ',width=10,command=self.close) self.closeButton.pack(expand=1,side=Tkinter.RIGHT,padx=15,pady=8) self.frame[3].pack(expand=1,fill=Tkinter.BOTH) #接收消息 def receiveMessage(self): try: #建立Socket連接 self.clientSock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.clientSock.connect((self.local, self.port)) self.flag = True except: self.flag = False self.chatText.insert(Tkinter.END,'您還未與服務器端建立連接,請檢查服務器端是否已經啟動') return self.buffer = 1024 self.clientSock.send('Y') while True: try: if self.flag == True: #連接建立,接收服務器端消息 self.serverMsg = self.clientSock.recv(self.buffer) if self.serverMsg == 'Y': self.chatText.insert(Tkinter.END,'客戶端已經與服務器端建立連接......') elif self.serverMsg == 'N': self.chatText.insert(Tkinter.END,'客戶端與服務器端建立連接失敗......') elif not self.serverMsg: continue else: theTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) self.chatText.insert(Tkinter.END, '服務器 ' + theTime +' 說:/n') self.chatText.insert(Tkinter.END, ' ' + self.serverMsg) else: break except : self.chatText.insert(Tkinter.END,'出現錯誤......') self.clientSock.close() self.close() #發送消息 def sendMessage(self): #情況1:從text_input獲取用戶輸入 #得到用戶在Text中輸入的消息 message = self.inputText.get('1.0',Tkinter.END) #格式化當前的時間 theTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) self.chatText.insert(Tkinter.END, '客戶端 ' + theTime +' 說:/n') self.chatText.insert(Tkinter.END,' ' + message + '/n') if self.flag == True: #將消息發送到服務器端 self.clientSock.send(message); else: #Socket連接沒有建立,提示用戶 self.chatText.insert(Tkinter.END,'您還未與服務器端建立連接,服務器端無法收到您的消息/n') #清空用戶在Text中輸入的消息 self.inputText.delete(0.0,message.__len__()-1.0) #連接數據庫并操作 #def connectDatabaseRequest(self): #點擊小提示請求server連接數據庫 #正常打字請求server回復 #關閉消息窗口并退出 def close(self): sys.exit() #啟動線程接收服務器端的消息 def startNewThread(self): #啟動一個新線程來接收服務器端的消息 #thread.start_new_thread(function,args[,kwargs])函數原型, #其中function參數是將要調用的線程函數,args是傳遞給線程函數的參數,它必須是個元組類型,而kwargs是可選的參數 #receiveMessage函數不需要參數,就傳一個空元組 thread.start_new_thread(self.receiveMessage,())def main(): client = ClientUI() client.startNewThread() client.root.mainloop() if __name__=='__main__': main()3、效果圖(好大!!!)


4、缺陷:
很多,比如tkinter界面太糟糕,交互過程太簡潔而且不合理,沒有認證過程,沒有加入多線程并發等等一大堆問題。
甚至有些代碼注釋還沒改,算了,正學django,以后弄一個完整的過程吧。
新聞熱點
疑難解答