Skip to content
Advertisement

Everytime button is clicked store value in a different variable python tkinter

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 :

from tkinter import *  #GUI library a
import sys

root = Tk() #create a tkinter object which represents the parent window

root.title("Chat Bot")
root.geometry("400x500")
root.resizable(width=FALSE, height=FALSE)

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
chatWindow.place(x=6,y=6, height=385, width=370)

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
messageWindow.place(x=128, y=400, height=88, width=260)

def get():
    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.
    messageWindow.delete('1.0', END)

Button= Button(root, text="Send", command = get, width="12", height=5,
                    bd=0, bg="#0080ff", activebackground="#00bfff",foreground='#ffffff',font=("Arial", 12))
Button.place(x=6, y=400, height=88) #create button to send the message and place it in the parent window


#Whenever I am calling print) I am actually calling sys.stdout.write() so with this function redirector which redirect the print() to the GUI
def redirector(inputStr):
    chatWindow.insert(INSERT, inputStr)

sys.stdout.write = redirector #whenever sys.stdout.write is called, redirector is called.

print("Hello, I am your awesome assistant today. n")
print("Please enter your name and last name. n")
root.mainloop()

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:

import sys

name = ""
last_name = ""


def react(text):
    global name
    global last_name
    if len(name) == 0:
        name = text
    elif len(last_name) == 0:
        last_name = text
        print(f"hello {name} {last_name}")


def get():
    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.
    messageWindow.delete('1.0', END)
    name = name[0:-1]
    react(name)


# Whenever I am calling print() I am actually calling sys.stdout.write() so with this function redirector which redirect the print() to the GUI
def redirector(inputstr):
    chatWindow.insert(INSERT, inputstr)


sys.stdout.write = redirector  # whenever sys.stdout.write is called, redirector is called.


root = Tk()  # create a tkinter object which represents the parent window


root.title("Chat Bot")
root.geometry("400x500")
root.resizable(width=FALSE, height=FALSE)


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
chatWindow.place(x=6, y=6, height=385, width=370)


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
messageWindow.place(x=128, y=400, height=88, width=260)


Button = Button(root, text="Send", command=get, width="12", height=5, bd=0, bg="#0080ff", activebackground="#00bfff", foreground='#ffffff', font=("Arial", 12))
Button.place(x=6, y=400, height=88)  # create button to send the message and place it in the parent window


print("Hello, I am your awesome assistant today. n")
print("Please enter your name and last name. n")
root.mainloop()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement