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

就當(dāng)是女友無(wú)聊的時(shí)候自己抽不出時(shí)間的小分身吧!
需要用到的庫(kù)
tkinter、time、urllib、requests
tkinter負(fù)責(zé)窗體、time顯示時(shí)間、urllib和requests負(fù)責(zé)請(qǐng)求
窗體設(shè)計(jì)
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,' 歡迎來(lái)到你與男神的小天地!\n你可以把你想說(shuō)的內(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ā)送時(shí)間及顏色處理,使用requests和urllib庫(kù)來(lái)調(diào)用接口處理回復(fù)你女朋友的信息。
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("智能回復(fù)>>", res)
return res
為粉絲們額外添加的功能
根據(jù)女友輸入的內(nèi)容自定義回復(fù)
我自己添加的內(nèi)容太肉麻的,不太適合分享給你們哈。于是我就給你們做多了一個(gè)自定義回復(fù)的功能。嘿嘿!那就是獨(dú)一無(wú)二的代碼了
堅(jiān)持著你們直接能使用代碼不做任何修改的原則,我就不讓你們?cè)诖a里面添加了,當(dāng)你第一次運(yùn)行此代碼的時(shí)候會(huì)自動(dòng)創(chuàng)建一個(gè)txt文件(甚至還不用讓你創(chuàng)建文件),你就可以在txt文件中自定義回復(fù)內(nèi)容了。
使用異常處理模塊try來(lái)嘗試open讀取名為“自定義回復(fù).txt”的文件,若不存在except就創(chuàng)建,若已存在直接讀取即可。操作讀取的字符串逐個(gè)添加到zidingyi字典中,判斷輸入的語(yǔ)句是否在zidingyi.keys()中即可做出相應(yīng)回復(fù)。
添加后的效果圖:

zidingyi = {}
try:
with open("自定義回復(fù).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("自定義回復(fù).txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“輸入:回復(fù)”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(fù)(注意使用英文的冒號(hào))')
with open("自定義回復(fù).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("智能回復(fù)>>", res)
return res
zidingyi = {}
try:
with open("自定義回復(fù).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("自定義回復(fù).txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“輸入:回復(fù)”格式 如——>你吃飯了嗎?:我吃飯啦 回車以繼續(xù)下一自定義回復(fù)(注意使用英文的冒號(hào))')
with open("自定義回復(fù).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, ' 歡迎來(lái)到你與男神的小天地!\n 你可以把你想說(shuō)的內(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()怎么樣,是不是特別簡(jiǎn)單~快復(fù)制去送給你心中的那個(gè)女神吧~
以上就是基于Python制作AI聊天軟件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python聊天軟件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了Pytorch可視化(顯示圖片)及格式轉(zhuǎn)換問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python對(duì)象轉(zhuǎn)換為json的方法步驟
這篇文章主要介紹了Python對(duì)象轉(zhuǎn)換為json的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python灰度變換中的對(duì)數(shù)變換專項(xiàng)分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變?cè)磮D像中每個(gè)像素灰度值的方法。目的是改善畫質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非常基礎(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10
Python圖像運(yùn)算之頂帽運(yùn)算和底帽運(yùn)算詳解
數(shù)學(xué)形態(tài)學(xué)是應(yīng)用于圖像處理和模式識(shí)別領(lǐng)域的新方法。數(shù)學(xué)形態(tài)學(xué)表示以形態(tài)為基礎(chǔ)對(duì)圖像進(jìn)行分析的數(shù)學(xué)工具,基本思想是用具有一定形態(tài)的結(jié)構(gòu)元素去量度和提取圖像中對(duì)應(yīng)形狀以達(dá)到對(duì)圖像分析和識(shí)別的目的。本文將為大家介紹頂帽運(yùn)算和底帽運(yùn)算,需要的可以參考一下2022-07-07
Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機(jī)的方法
這篇文章主要介紹了Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機(jī)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Python tempfile模塊生成臨時(shí)文件和臨時(shí)目錄
這篇文章主要介紹了Python tempfile模塊生成臨時(shí)文件和臨時(shí)目錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能
手機(jī)上突然收到了某銀行的短信提示,看了一下手機(jī)的位數(shù),正好是11位,我一想,這不就是標(biāo)準(zhǔn)的手機(jī)號(hào)碼嗎?于是想用python的庫(kù)實(shí)現(xiàn)查詢手機(jī)號(hào)碼歸屬地查詢自由,所以本文給大家介紹了如何用python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能,需要的朋友可以參考下2024-03-03
sklearn線性邏輯回歸和非線性邏輯回歸的實(shí)現(xiàn)
這篇文章主要介紹了sklearn線性邏輯回歸和非線性邏輯回歸的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

