Hello I am trying to write a chatbot which takes the follow input : name and last name The idea is that when the user click the “Send” button the input is stored in a “name” variable. The text box is cleared and when the user click the button again the new input should go into another variable “lastname”. How can I achieve that? I would like to print “Hello name + lastname” inside the chatbot’s GUI
This is what I have so far :
JavaScript
x
34
34
1
from tkinter import * #GUI library a
2
import sys
3
4
root = Tk() #create a tkinter object which represents the parent window
5
6
root.title("Chat Bot")
7
root.geometry("400x500")
8
root.resizable(width=FALSE, height=FALSE)
9
10
chatWindow = Text(root, bd=1, bg="black", width="50", height="8", font=("Arial", 12), foreground="#00ffff") #create a window for the conversation and place it on the parent window
11
chatWindow.place(x=6,y=6, height=385, width=370)
12
13
messageWindow = Text(root, bd=0, bg="black",width="30", height="4", font=("Arial", 23), foreground="#00ffff") #create the text area where the message will be entered and place it on the parent window
14
messageWindow.place(x=128, y=400, height=88, width=260)
15
16
def get():
17
name = messageWindow.get('1.0', END) #"1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached.
18
messageWindow.delete('1.0', END)
19
20
Button= Button(root, text="Send", command = get, width="12", height=5,
21
bd=0, bg="#0080ff", activebackground="#00bfff",foreground='#ffffff',font=("Arial", 12))
22
Button.place(x=6, y=400, height=88) #create button to send the message and place it in the parent window
23
24
25
#Whenever I am calling print) I am actually calling sys.stdout.write() so with this function redirector which redirect the print() to the GUI
26
def redirector(inputStr):
27
chatWindow.insert(INSERT, inputStr)
28
29
sys.stdout.write = redirector #whenever sys.stdout.write is called, redirector is called.
30
31
print("Hello, I am your awesome assistant today. n")
32
print("Please enter your name and last name. n")
33
root.mainloop()
34
Advertisement
Answer
I looked at your problem and organized your code. you can solve the problem by creating another function and calling it in get()
. you can check if a variable is empty with the stament len()
. then your code will be like this:
JavaScript
1
55
55
1
import sys
2
3
name = ""
4
last_name = ""
5
6
7
def react(text):
8
global name
9
global last_name
10
if len(name) == 0:
11
name = text
12
elif len(last_name) == 0:
13
last_name = text
14
print(f"hello {name} {last_name}")
15
16
17
def get():
18
name = messageWindow.get('1.0', END) # "1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached.
19
messageWindow.delete('1.0', END)
20
name = name[0:-1]
21
react(name)
22
23
24
# Whenever I am calling print() I am actually calling sys.stdout.write() so with this function redirector which redirect the print() to the GUI
25
def redirector(inputstr):
26
chatWindow.insert(INSERT, inputstr)
27
28
29
sys.stdout.write = redirector # whenever sys.stdout.write is called, redirector is called.
30
31
32
root = Tk() # create a tkinter object which represents the parent window
33
34
35
root.title("Chat Bot")
36
root.geometry("400x500")
37
root.resizable(width=FALSE, height=FALSE)
38
39
40
chatWindow = Text(root, bd=1, bg="black", width="50", height="8", font=("Arial", 12), foreground="#00ffff") # create a window for the conversation and place it on the parent window
41
chatWindow.place(x=6, y=6, height=385, width=370)
42
43
44
messageWindow = Text(root, bd=0, bg="black", width="30", height="4", font=("Arial", 23), foreground="#00ffff") # create the text area where the message will be entered and place it on the parent window
45
messageWindow.place(x=128, y=400, height=88, width=260)
46
47
48
Button = Button(root, text="Send", command=get, width="12", height=5, bd=0, bg="#0080ff", activebackground="#00bfff", foreground='#ffffff', font=("Arial", 12))
49
Button.place(x=6, y=400, height=88) # create button to send the message and place it in the parent window
50
51
52
print("Hello, I am your awesome assistant today. n")
53
print("Please enter your name and last name. n")
54
root.mainloop()
55