为什么我的函数没有返回任何内容?

时间:2016-09-06 11:45:14

标签: python tkinter

我正在尝试使用python和tkinter制作一个基本的聊天机器人,并遇到了一个问题。为简单起见,我排除了tkinter代码。整个代码在底部可见。

 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()

输入被输入到一个输入框中,并被发送到通信功能,该功能将输入发送到bottalk函数,该函数应该设置对" hello received"的响应。或者"你好没收到",并更新GUI上的标签。但是,当我这样做时,标签不会改变,控制台输出似乎是两个空行。 为什么我的功能没有设置响应"你好收到"或者"你好没收到",如果是,为什么不打印这个或更新GUI?

打印(AI_RESPONSE)导致Py-Var2显示输出2个空白行。我的问题不在于该行。

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 个答案:

答案 0 :(得分:3)

由于响应作为值返回,因此无法更新"type":"multi", "folders": [ "cities/", "users/" ] 函数内的response变量。您需要使用函数返回的值更新communicate

response

答案 1 :(得分:1)

responseStringVar,因此您必须使用.set(text)代替=

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

现在您不必返回值,也不需要使用global。你会在标签和控制台中看到文字。

答案 2 :(得分:0)

从一开始,我认为您的代码中几乎没有错误:

    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()

你在构造函数中没有任何东西,也许你应该把代码放在那里:

    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

可能你想"清洁"在对象之后,我建议您阅读更多相关内容,例如:How do I correctly clean up a Python object?
另外你的另一种方法:

java.io.Console

我几乎没有建议您首先阅读有关python编程的基础知识,而不是开始使用一些高级模块。我想在此重定向您:https://docs.python.org/3/tutorial/classes.html