0

I am attempting to make a rudimentary chatbot using python and tkinter, and have ran into an issue. I have excluded tkinter code for simplicity. The entire code is visible at the bottom.

 def communicate():
        sent.set(HUMAN_ENTRY.get())
        bottalk(response)

        AI_RESPONSE.set(response.get())           
        print (response.get())            
        print(AI_RESPONSE.get())
        root.update()

 def bottalk(response):
        if sent == 'hello': 
            response = 'hello recieved'
        else:
           response = 'hello not recieved'
        return response

 AI_RESPONSE = 'hellgeto'
 header.pack()
 sent = StringVar()
 response = StringVar()
 AI_RESPONSE = StringVar()

An input is made into a entry box, and is sent to the communicate function, which sends the input to the bottalk function, which should set response to either "hello received" or "hello not received", and update a label on the GUI. However, when I do this, the label does not change, and the console outputs what seems to be two blank lines. Why is my function not setting response to either "hello received" or "hello not received", and if it is, why is it not printing this or updating the GUI?

Print(AI_RESPONSE) resulting in Py-Var2 was to show that 2 blank lines out outputted. My question does not concern that line.

from tkinter import *
import random


class App:
    def __init__(self, master):

    def close():
        quit()

    def communicate():
        sent.set(HUMAN_ENTRY.get())
        bottalk(response)

        AI_RESPONSE.set(response.get())           
        print (response.get())            
        print(AI_RESPONSE.get())
        print(AI_RESPONSE)
        root.update()

    def bottalk(response):
        if sent == 'hello': 
            response = 'hello recieved'
        else:
            response = 'hello not recieved'
        return response

    AI_RESPONSE = 'hellgeto'
    root.title=('GoBot')
    frame = Frame(master)
    frame.pack()
    self.button = Button(frame,text='Send', command=communicate)
    self.button.pack(side=LEFT)
    self.button2 = Button(frame,text='Quit', command=close)
    self.button2.pack(side=RIGHT)
    header = Label(frame, text='GoBot', fg= 'blue', font = 'Times')
    header.pack()
    sent = StringVar()
    response = StringVar()
    AI_RESPONSE = StringVar()
    HUMAN_ENTRY = Entry(master, bd = 5)
    HUMAN_ENTRY.pack(side=RIGHT)
    responselabel = Label(frame, textvariable=AI_RESPONSE, fg = 'purple', font = 'ComicSans', anchor ='s')
    responselabel.pack()




root = Tk()
app = App(root)      
root.mainloop()

The Console

3 Answers3

3

Since response returned as value, it won't update the response variable inside communicate function. You need to update response with the value returned from the function:

def communicate():
    sent.set(HUMAN_ENTRY.get())
    response = bottalk(response)

    AI_RESPONSE.set(response.get())           
    print (response.get())            
    print(AI_RESPONSE.get())
    root.update()
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
  • This results in an error: "UnboundLocalError: local variable 'response' referenced before assignment" I assume I need to set the variable globally, but where in tkinter is that doable? – BritishFerret Sep 06 '16 at 11:54
1

response is StringVar so you have to use .set(text) instead of =

def bottalk(response):
    if sent == 'hello': 
        response.set('hello recieved')
    else:
        response.set('hello not recieved')

And now you don't have to return value, and don't need to use global. And you see text in label and console.

furas
  • 134,197
  • 12
  • 106
  • 148
0

Ok from beginning, i think that you have few mistakes in your code:

class App:
    def __init__(self, master):

You dont have anything in constructor, maybe you should put below code there:

    AI_RESPONSE = 'hellgeto'
    root.title=('GoBot')
    frame = Frame(master)
    frame.pack()
    self.button = Button(frame,text='Send', command=communicate)
    self.button.pack(side=LEFT)
    self.button2 = Button(frame,text='Quit', command=close)
    self.button2.pack(side=RIGHT)
    header = Label(frame, text='GoBot', fg= 'blue', font = 'Times')
    header.pack()
    sent = StringVar()
    response = StringVar()
    AI_RESPONSE = StringVar()
    HUMAN_ENTRY = Entry(master, bd = 5)
    HUMAN_ENTRY.pack(side=RIGHT)
    responselabel = Label(frame, textvariable=AI_RESPONSE, fg = 'purple', font = 'ComicSans', anchor ='s')
    responselabel.pack()

Next method:

    def close():
        quit()

probably you want to "clean" after object, then i recommend to read more about this, for example here: How do I correctly clean up a Python object?
Also your another methods:

    def communicate():
        sent.set(HUMAN_ENTRY.get())
        bottalk(response)

        AI_RESPONSE.set(response.get())           
        print (response.get())            
        print(AI_RESPONSE.get())
        print(AI_RESPONSE)
        root.update()

    def bottalk(response):
        if sent == 'hello': 
            response = 'hello recieved'
        else:
            response = 'hello not recieved'
        return response

I hardly recommend you first at all read about basics of python programming rather then start using some advanced modules. I want to redirect you here: https://docs.python.org/3/tutorial/classes.html

Community
  • 1
  • 1
  • Not strictly answering the question (i've got what I asked down) , but I do appreciate the tips and redirections. I will certainly look into all of these. Thank you! – BritishFerret Sep 06 '16 at 12:25