如何解决关于如何实现聊天机器人到 GUI 的困惑
我最近使用 Python 制作了一个聊天机器人,并使用 Tkinter 为我的聊天机器人制作了 GUI。但问题是我不知道如何在 GUI 中实现我的聊天机器人。就像我分别制作了聊天机器人和 GUI 一样。如果你们能帮助我,我真的很感激。下面分别是 chatbot 和 GUI 代码:
import abilities as ab
import json
print("hey! I'm your new personel assistant!")
name = input("May I kNow your good name please:").strip()
for i in range(len(ab.noname)):
if ab.noname[i] in name.lower() or len(name) == 0:
print("Its okay...I respect your privacy:)")
name = ''
break
else:
if name != '':
print(f"Nice to meet you {name},so let's get started")
def main():
print(f"Here's a list of activities I can do...take a look!")
print(json.dumps(ab.tasklist,indent=4))
print('\n')
query = input(f"So {name},what can I do for you Now:").strip()
choice = 0
for i in ab.keyword:
if choice == 0:
for j in ab.keyword[i]:
if j in query:
choice = i
break
else:
break
# Performing the activities
if choice == 1:#opening website
try:
ab.search()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while a := (input("Want to search some more websites?:").lower().strip()[0]) in ['y','o','a','k']:
try:
ab.search()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
else:
main()
elif choice == 2:#Playing Music
try:
ab.music()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while b := (input("Want to listen to some more music?:").lower().strip()[0]) in ['y','k']:
try:
ab.music()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
else:
main()
elif choice == 3:#Whatsapp
try:
ab.whatsapp()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while c := (input("Anyone more to be texted:").lower().strip()[0]) in ['y','k']:
# Smart way using Walrus operator
try:
ab.whatsapp()
except:
print("I think you aren't connected to any networks. Or try reading the question properly")
main()
else:
main()
elif choice == 4:#googlesearch
try:
ab.googlesearch()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while f := (input("Do you want me to search more stuffs for you?:").lower().strip()[0]) in ['y','k']:
try:
ab.googlesearch()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
else:
main()
elif choice == 5:#news
print("opening the virtual newspaper")
try:
ab.news()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while d := (input("Do you wish to read the news again?:").lower().strip()[0]) in ['y','k']:
try:
ab.news()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
else:
main()
elif choice == 6:
ab.curdate()
main()
elif choice == 7:
try:
ab.wikisearch()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
while e := (input("Don't you want to stay longer in Wikipedia?:").lower().strip()[0]) in ['y','k']:
try:
ab.wikisearch()
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
main()
else:
main()
elif choice == 8:
sec = int(input("Enter the time in seconds after which you want me to shutdown your pc for you:"))
if input("Wait! Are you sure about this?").lower().strip()[0] in ['y','k']:
ab.pcshutd(sec)
else:
main()
elif choice == 9:
ab.byebye(name)
else:
print("Sorry I'm unable to satisfy your request")
main()
main()
#abilities
import webbrowser
import json
import pywhatkit
from datetime import datetime
#1) Abilities Using webbrowser
def search():#implemented
print("\n")
wbchoice = {1: "default webbrowser",2: "chrome"}
defbrwser = ["def","1"]
chrbrwser = ["chr","google","2"]
print(json.dumps(wbchoice,indent=3))#The only place where we use Json and that is to organize the dictionary
wbc = input("So which browser do you like?:")
website = input("Now enter the name of the website (only) which you wish to directly access:").lower() # entering the name of website
url = f"{website}.com"
for i in range(len(defbrwser)):
if defbrwser[i] in wbc: # for default webbrowser
webbrowser.open_new(url)
break
for i in range(len(chrbrwser)):
if chrbrwser[i] in wbc: # for Chrome
chromepath = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
webbrowser.register('chrome',None,webbrowser.Backgroundbrowser(chromepath))
webbrowser.get("chrome").open_new(url)
break
def news():#implemented
webbrowser.open_new("https://news.google.com/topstories?hl=en-IN&gl=IN&ceid=IN%3Aen")
#2) Abilities Using pywhatkit
def music():#implemented
song = input("Awsomeee,so which song do you want to listen to?:") # entering the name of website
pywhatkit.playonyt(song)
def whatsapp():#implemented
print("Okayyy cool,so Now you can just give me a few details and we r good to go",'\n')
phoneno=input("Enter the phone number of the receiver & also make sure you add the country code with + :")
mssge=input("Enter the message:")
pywhatkit.sendwhatmsg_instantly(phoneno,mssge)
def pcshutd(sec):#implemented
pywhatkit.shutdown(time=sec)
def wikisearch():#implemented
topic=input("Okay,so tell me the topic you want to search on Wikipedia:")
lines=int(input("Also enter the number of lines to be printed based on the topic:"))
try:
pywhatkit.info(topic,lines)
except:
print("No such article based on the topic given exists on Wikipedia. Try rephrasing/shortening the topic! ")
def googlesearch():#implemented
info = input("Enter whatever you want me to find for you:")
try:
pywhatkit.search(info)
except:
print("I think you aren't connected to any networks. Please try again later with a good newtork!")
#3) Abilities using datetime function from datetime module
def curdate():#implemented
print("Date and Time Now:",datetime.Now())
#4) Abilities to Answer personal question
def byebye(name):
print(f"I hope you enjoyed using me as much as I did helping you out.so thank you {name} :). I will see you when I see you ~ metis")
#Removing unneccessary special characters for search algo
noname=['no','nope','sorry',"wont","cant","dont","aint","will not","never"]
defbrwser = ["def"]
chrbrwser = ["chr","google"]
firefox = ["fire","moz"]
#Task list
tasklist = {1: "To enter a website directly",2: "Fill your ears with some sick music",3: "Whatsapp you family,friends and foes instantly",4: "To google search something",5: "Show you today\'s highlights and updates",6: "display current date and time",7: "Search the Wikipedia",8: "Shutdown your pc within a specified number of seconds",}
#Keyword List
keyword = {1: ["website",'1'],2: ["music","musics","listen","songs","listen to music","play music","hear","melodys","song","2"],3: ["whatsapp","text",'message',"msg","3"],4: ["google","4"],5: ["highlights","updates","news","5"],6: ["todays date and time","date","time","6"],7: ["wikipedia","wiki","7"],8: ["shutdown",'shut',"off my pc","turndown pc","8"],9: ["leave","bye","good night","seeya","take care","close","terminate","stop","nothing","leaving","going","ttyl","9","no"]
}
现在终于有了我使用 Tkinter 为我的聊天机器人创建的 GUI 代码:
from tkinter import *
from PIL import ImageTk,Image
def send():
print('send')
def quit():
print('quit')
def start():
print('start')
def second_window():
root=Tk()
root.title('welcome to metis')
root.geometry('400x500')
label2=Label(root,text='welcome to metis')
button2=Button(root,text='hi').place(x=0,y=0)
file_menu = Menu(root)
main_menu = Menu(root)
file_menu.add_command(label="New..")
file_menu.add_command(label="Save As..")
file_menu.add_command(label="Exit")
main_menu.add_cascade(label="File",menu=file_menu)
main_menu.add_command(label="Quit",command=root.quit)
root.config(menu=main_menu)
chatwindow = Text(root,bd=1,bg="black",width="50",height="8",font=("Arial",23),foreground="#00ffff")
chatwindow.place(x=6,y=6,height=385,width=370)
messageWindow = Text(root,bd=0,width="30",height="4",foreground="#00ffff")
messageWindow.place(x=128,y=400,height=88,width=260)
scrollbar = Scrollbar(root,command=chatwindow.yview,cursor="star")
scrollbar.place(x=375,y=5,height=385)
sendphoto=PhotoImage(file='send.png')
Button3= Button(root,text="Send",width="12",height=5,command=send,bg="#0080ff",activebackground="#00bfff",foreground='#ffffff',12))
Button3.place(x=6,height=88)
root.mainloop()
window=Tk()
window.geometry('500x497')
window.title('metis')
window.config(bg='blue')
bg=PhotoImage(file='rob111.png')
myimage=ImageTk.PhotoImage(Image.open('png-format-16265000621410769899metis.png'))
label=Label(window,image=bg)
label.place(x=0,y=0)
icon=PhotoImage(file='rob111.png')
window.iconphoto(True,icon)
button1=Button(window,text='start',command=second_window).place(x=250,y=250)
window.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。