基于Python制作AI聊天軟件的示例代碼
效果圖
先看一下效果圖

就當是女友無聊的時候自己抽不出時間的小分身吧!
需要用到的庫
tkinter、time、urllib、requests
tkinter負責窗體、time顯示時間、urllib和requests負責請求
窗體設(shè)計
from tkinter import *
win1 = Tk()
win1.geometry('400x644+100+100')
win1.title('xxx男神的AI分身')
Label11 = Label(win1, text='男神白', font=('黑體', 12), anchor='center').place(y=13, x=15, width=380, height=20)
Entry11 = Entry(win1, font=('等線', 11), width = 70)
Entry11.place(y=600, x=15, width=310, height=26)
Button11 = Button(win1, text='發(fā)送', font=('等線', 11), command = mecha).place(y=598, x=328, width=65, height=30)
console = Text(win1, font=('等線', 11))
console.place(y=35, x=15, width=368, height=550)
console.insert(1.0,' 歡迎來到你與男神的小天地!\n你可以把你想說的內(nèi)容輸入到下面的輸入框哦\n')
console.mark_set('markOne', 1.0)
console.mark_set('markTwo', 3.0)
console.tag_add('tag1', 'markOne', 'markTwo')
console.tag_config('tag1', foreground='red')
win1.bind("<Return>", test_fun)
win1.mainloop()
函數(shù)
在txet部件上顯示發(fā)送時間及顏色處理,使用requests和urllib庫來調(diào)用接口處理回復你女朋友的信息。
def liaotian():
global b
import time
b = 3
def mecha():
global b
b+=2
console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end',str(Entry11.get())+'\n')
console.mark_set(str(b-1), str(b-1)+'.0')
console.mark_set(str(b), str(b)+'.0')
console.tag_add(str(b), str(b-1), str(b))
console.tag_config(str(b), foreground='blue')
console.see(END)
console.update()
console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end',aicha()+'\n')
console.mark_set(str(b-1), str(b-1)+'.0')
console.mark_set(str(b), str(b)+'.0')
console.tag_add(str(b), str(b-1), str(b))
console.tag_config(str(b), foreground='red')
console.see(END)
Entry11.delete(0,END)
console.update()
def test_fun(self):
mecha()
def aicha():
global b
b+=2
msg = str(Entry11.get())
else:
import urllib
import requests
def qingyunke(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
print("原話>>", msg)
res = qingyunke(msg)
res = res.replace('菲菲', '你男神')
res = res.replace('我', '你男神')
print("智能回復>>", res)
return res
為粉絲們額外添加的功能
根據(jù)女友輸入的內(nèi)容自定義回復
我自己添加的內(nèi)容太肉麻的,不太適合分享給你們哈。于是我就給你們做多了一個自定義回復的功能。嘿嘿!那就是獨一無二的代碼了
堅持著你們直接能使用代碼不做任何修改的原則,我就不讓你們在代碼里面添加了,當你第一次運行此代碼的時候會自動創(chuàng)建一個txt文件(甚至還不用讓你創(chuàng)建文件),你就可以在txt文件中自定義回復內(nèi)容了。
使用異常處理模塊try來嘗試open讀取名為“自定義回復.txt”的文件,若不存在except就創(chuàng)建,若已存在直接讀取即可。操作讀取的字符串逐個添加到zidingyi字典中,判斷輸入的語句是否在zidingyi.keys()中即可做出相應回復。
添加后的效果圖:

zidingyi = {}
try:
with open("自定義回復.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
except:
with open("自定義回復.txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“輸入:回復”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(注意使用英文的冒號)')
with open("自定義回復.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd[1:]:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
print(line)
print(zidingyi)
完整代碼
from tkinter import *
def liaotian():
global b
import time
b = 3
def mecha():
global b
b += 2
console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end', str(Entry11.get()) + '\n')
console.mark_set(str(b - 1), str(b - 1) + '.0')
console.mark_set(str(b), str(b) + '.0')
console.tag_add(str(b), str(b - 1), str(b))
console.tag_config(str(b), foreground='blue')
console.see(END)
console.update()
console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end', aicha() + '\n')
console.mark_set(str(b - 1), str(b - 1) + '.0')
console.mark_set(str(b), str(b) + '.0')
console.tag_add(str(b), str(b - 1), str(b))
console.tag_config(str(b), foreground='red')
console.see(END)
Entry11.delete(0, END)
console.update()
def test_fun(self):
mecha()
def aicha():
global b
b += 2
msg = str(Entry11.get())
if msg in zidingyi.keys():
res = zidingyi[msg]
return res
else:
import urllib
import requests
def qingyunke(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
print("原話>>", msg)
res = qingyunke(msg)
res = res.replace('菲菲', '你男神')
res = res.replace('我', '你男神')
print("智能回復>>", res)
return res
zidingyi = {}
try:
with open("自定義回復.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
except:
with open("自定義回復.txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“輸入:回復”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(注意使用英文的冒號)')
with open("自定義回復.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd[1:]:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
print(line)
print(zidingyi)
win1 = Tk()
win1.geometry('400x644+100+100')
win1.title('男神的AI分身')
Label11 = Label(win1, text='你男神', font=('黑體', 12), anchor='center').place(y=13, x=15, width=380, height=20)
Entry11 = Entry(win1, font=('等線', 11), width=70)
Entry11.place(y=600, x=15, width=310, height=26)
Button11 = Button(win1, text='發(fā)送', font=('等線', 11), command=mecha).place(y=598, x=328, width=65, height=30)
console = Text(win1, font=('等線', 11))
console.place(y=35, x=15, width=368, height=550)
console.insert(1.0, ' 歡迎來到你與男神的小天地!\n 你可以把你想說的內(nèi)容輸入到下面的輸入框哦\n')
console.mark_set('markOne', 1.0)
console.mark_set('markTwo', 3.0)
console.tag_add('tag1', 'markOne', 'markTwo')
console.tag_config('tag1', foreground='red')
win1.bind("<Return>", test_fun)
win1.mainloop()
liaotian()怎么樣,是不是特別簡單~快復制去送給你心中的那個女神吧~
以上就是基于Python制作AI聊天軟件的示例代碼的詳細內(nèi)容,更多關(guān)于Python聊天軟件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問題
這篇文章主要介紹了Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Python灰度變換中的對數(shù)變換專項分析實現(xiàn)
灰度變換是指根據(jù)某種目標條件按一定變換關(guān)系逐點改變源圖像中每個像素灰度值的方法。目的是改善畫質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個重要組成部分2022-10-10
Python Tricks 使用 pywinrm 遠程控制 Windows 主機的方法
這篇文章主要介紹了Python Tricks 使用 pywinrm 遠程控制 Windows 主機的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
sklearn線性邏輯回歸和非線性邏輯回歸的實現(xiàn)
這篇文章主要介紹了sklearn線性邏輯回歸和非線性邏輯回歸的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

