In: Computer Science
PYTHON PROGRAMMING
Using tktinker, create a gui that surrounds the code below that has buttons and labels and an overall theme.
# Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!') print(' ') print('Lets get started') #begin questions firstName = input('What is your first name?: ') lastName = input('What is your last name? ') print("Hi there, ", firstName + lastName, "nice to meet you") print(" ") # questions about users favorite things currentYear = 2020 birthDay = input('What is the year were you born?: ') birthDay = int(birthDay) print('Wow! You are already', currentYear - birthDay) print(" ") # questions about favorite hobbies firstHobby = input('Do you play any sports? ') if(firstHobby == 'Yes'): sport = input('What sport do you play? ') print("Nice! I played Soccer and Tennis back when I was human") print(" ") else: print("Bummer, I always liked sports") print(" ") favAnimal = input('What is your favorite animal? ') print("Oooohhh what a cool animal! My favorite is the red panda, such a unique and cute species!") print(" ") favSeason = input('What is your favorite season? ') if(favSeason == 'Fall'): print("Wow! That is my favorite season also! The colors of the leaves are amazing") print(" ") else: print("Nice! My favorite season is Fall!") print(" ") siblings = input('Do you have any siblings in your family? ') if(siblings == 'Yes'): print("Very nice, back when I was born a human I grew up with a sister") print(" ") else: print("An only child I see") print(" ") stateBorn = input('What state were you born in? ') if(stateBorn == 'West Virginia'): print("My creator is also from there!") print(" ") else: print("What a lovely state") print(" ") #end questions print("Well I thank you for your time but that is all the time we have, thank you for joining me today")
Programmed using PyCharm, After each entry press 'Enter Button'.
Code:
from tkinter import *
window=Tk() # Initialize tkinter window
window.title("ChatBot") # Title of the window
window.geometry("480x900") # Geometry of the Window
displayState = 1 # Initialize displaystate
def onClick(): # Onclick Function
global displayState # Declare variable as global
if displayState == 1: # If displayState is 1
window.first = e1.get() # Get Values from e1 entry
window.last = e2.get() # Get Values from e2 entry
label5 = Label(window, text=f"Hi there,{window.first} {window.last}, nice to meet you", font="times 12").place(x=5, y=175) # Label
label6 = Label(window, text="What is the year were you born?", font="times 12").place(x=5, y=200) # Label
window.e3 = Entry(window, width=25) # Entry window
window.e3.place(x=5, y=225) # Placement of entry box
elif displayState == 2:
year = window.e3.get() # Get Values from e3 entry
currentYear = 2020 # Set Value of currentYear
age = currentYear - int(year) # Calculate Age
label7 = Label(window, text=f"Wow! You are already', {age}", font="times 12").place(x=5, y=250) # Label
label8 = Label(window, text="Do you play any sports?", font="times 12").place(x=5, y=275) # Label
window.e4 = Entry(window, width=25) # Entry window
window.e4.place(x=5, y=300) # Placement of entry box
elif displayState == 3:
sport = window.e4.get() # Get Values from e4 entry
if sport == 'Yes' or sport == 'yes': # If sport is yes
label9 = Label(window, text="What sport do you play??", font="times 12").place(x=5, y=325) # Label
e5 = Entry(window, width=25) # Entry window
e5.place(x=5, y=350) # Placement of entry box
window.cl = 1 # set value of variable
elif sport == 'No' or sport == 'no': # If sport is no
label9 = Label(window, text="Bummer, I always liked sports", font="times 12").place(x=5, y=325) # Label
label11 = Label(window, text="What is your favorite animal?", font="times 12").place(x=5, y=350) # Label
window.e6 = Entry(window, width=25) # Entry window
window.e6.place(x=5, y=375) # Placement of entry box
window.cl = 0 # set value of variable
elif displayState == 4:
if window.cl == 1:
label10 = Label(window, text="Nice! I played Soccer and Tennis back when I was human", font="times 12").place(x=5, y=375) # Label
label11 = Label(window, text="What is your favorite animal?", font="times 12").place(x=5, y=400) # Label
window.e6 = Entry(window, width=25) # Entry window
window.e6.place(x=5, y=425) # Placement of entry box
elif window.cl == 0:
label12 = Label(window, text="Oooohhh what a cool animal! My favorite is the red panda,\n such a unique and cute species!", font="times 12").place(x=5, y=400) # Label
label13 = Label(window, text="What is your favorite season?", font="times 12").place(x=5, y=450) # Label
window.e7 = Entry(window, width=25) # Entry window
window.e7.place(x=5, y=475) # Placement of entry box
elif displayState == 5:
if window.cl == 1:
label12 = Label(window,
text="Oooohhh what a cool animal! My favorite is the red panda,\n such a unique and cute species!",
font="times 12", justify=LEFT).place(x=5, y=450) # Label
label13 = Label(window, text="What is your favorite season?", font="times 12").place(x=5, y=500) # Label
window.e7 = Entry(window, width=25) # Entry window
window.e7.place(x=5, y=525) # Placement of entry box
elif window.cl == 0:
season = window.e7.get()
if season == 'Fall':
label13 = Label(window, text="Wow! That is my favorite season also! The colors of the leaves are amazing", font="times 12").place(x=5, y=500) # Label
label14 = Label(window, text="Do you have any siblings in your family?", font="times 12").place(x=5, y=525) # Label
window.e8 = Entry(window, width=25) # Entry window
window.e8.place(x=5, y=550) # Placement of entry box
else:
label13 = Label(window,
text="Nice! My favorite season is Fall!",
font="times 12").place(x=5, y=500) # Label
label14 = Label(window, text="Do you have any siblings in your family?", font="times 12").place(x=5,
y=525) # Label
window.e8 = Entry(window, width=25) # Entry window
window.e8.place(x=5, y=550) # Placement of entry box
elif displayState == 6:
if window.cl == 1:
season = window.e7.get()
if season == 'Fall':
label13 = Label(window, text="Wow! That is my favorite season also! The colors of the leaves are amazing", font="times 12").place(x=5, y=550) # Label
label14 = Label(window, text="Do you have any siblings in your family?", font="times 12").place(x=5, y=575) # Label
window.e8 = Entry(window, width=25) # Entry window
window.e8.place(x=5, y=600) # Placement of entry box
else:
label13 = Label(window,
text="Nice! My favorite season is Fall!",
font="times 12").place(x=5, y=550) # Label
label14 = Label(window, text="Do you have any siblings in your family?", font="times 12").place(x=5,
y=575) # Label
window.e8 = Entry(window, width=25) # Entry window
window.e8.place(x=5, y=600) # Placement of entry box
if window.cl == 0:
siblings = window.e8.get()
if siblings == 'Yes' or siblings == 'yes':
label14 = Label(window, text="Very nice, back when I was born a human I grew up with a sister", font="times 12").place(x=5, y=575) # Label
label15 = Label(window, text="What state were you born in?", font="times 12").place(x=5, y=600) # Label
window.e9 = Entry(window, width=25) # Entry window
window.e9.place(x=5, y=625) # Placement of entry box
elif siblings == 'No' or siblings == 'no':
label14 = Label(window, text="An only child I see", font="times 12").place(x=5,y=575) # Label
label15 = Label(window, text="What state were you born in?", font="times 12").place(x=5, y=600) # Label
window.e9 = Entry(window, width=25) # Entry window
window.e9.place(x=5, y=625) # Placement of entry box
elif displayState == 7:
if window.cl == 1:
siblings = window.e8.get()
if siblings == 'Yes' or siblings == 'yes':
label14 = Label(window, text="Very nice, back when I was born a human I grew up with a sister", font="times 12").place(x=5, y=625) # Label
label15 = Label(window, text="What state were you born in?", font="times 12").place(x=5, y=650) # Label
window.e9 = Entry(window, width=25) # Entry window
window.e9.place(x=5, y=675) # Placement of entry box
elif siblings == 'No' or siblings == 'no':
label14 = Label(window, text="An only child I see", font="times 12").place(x=5,y=625) # Label
label15 = Label(window, text="What state were you born in?", font="times 12").place(x=5, y=650) # Label
window.e9 = Entry(window, width=25) # Entry window
window.e9.place(x=5, y=675) # Placement of entry box
if window.cl == 0:
state = window.e9.get()
if state == 'West Virginia':
label16 = Label(window, text="My creator is also from there!", font="times 12").place(x=5, y=650) # Label
label16 = Label(window, text="Well I thank you for your time but that is all the time we have,\n thank you for joining me today", font="times 12").place(x=5, y=675) # Label
else:
label16 = Label(window, text="What a lovely state", font="times 12").place(x=5, y=650) # Label
label16 = Label(window, text="Well I thank you for your time but that is all the time we have,\n thank you for joining me today", font="times 12").place(x=5, y=675) # Label
elif displayState == 8:
if window.cl == 1:
state = window.e9.get()
if state == 'West Virginia':
label16 = Label(window, text="My creator is also from there!", font="times 12").place(x=5, y=700) # Label
label16 = Label(window, text="Well I thank you for your time but that is all the time we have,\n thank you for joining me today", font="times 12").place(x=5, y=725) # Label
else:
label16 = Label(window, text="What a lovely state", font="times 12").place(x=5, y=700) # Label
label16 = Label(window, text="Well I thank you for your time but that is all the time we have,\n thank you for joining me today", font="times 12").place(x=5, y=725) # Label
displayState += 1
label1 = Label(window, text ="Hi there! Welcome to the ChatBot station.\nI am going to ask you a series of questions and all you have to do is answer!", font="times 12", justify=LEFT).place(x=5, y =5) # Label
label2 = Label(window, text ="Lets get started", font="times 12").place(x=5, y =50) # Label
label3 = Label(window, text ="What is your first name?", font="times 12").place(x=5, y =75) # Label
e1 = Entry(window, width=25) # get values of e1
e1.place(x=5, y=100) # Placement of entry box
label4 = Label(window, text="What is your last name?", font="times 12").place(x=5, y=125) # Label
e2 = Entry(window, width=25) # get values of e2
e2.place(x=5, y=150) # Placement of entry box
b1 = Button(window, text='Enter', width=30, command=onClick).place(x=100, y=800) # Button
window.mainloop()
Output: