[英]Why isn't my function returning anything?
我正在尝试使用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功能,该功能应将响应设置为“已收到您好”或“未收到您好”,并在GUI上更新标签。 但是,当我这样做时,标签不会更改,并且控制台会输出似乎是两行的空白行。 为什么我的函数未设置对“已收到问候”或“未收到问候”的响应,如果是,为什么不打印此信息或更新GUI?
导致Py-Var2的Print(AI_RESPONSE)显示输出了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()
由于响应作为值返回,因此它不会更新communicate
函数内部的response
变量。 您需要使用函数返回的值更新response
:
def communicate():
sent.set(HUMAN_ENTRY.get())
response = bottalk(response)
AI_RESPONSE.set(response.get())
print (response.get())
print(AI_RESPONSE.get())
root.update()
response
是StringVar
因此您必须使用.set(text)
而不是=
def bottalk(response):
if sent == 'hello':
response.set('hello recieved')
else:
response.set('hello not recieved')
现在,您不必返回值,也不需要使用global
。 您会在标签和控制台中看到文本。
好的,从一开始,我认为您的代码中几乎没有错误:
class App:
def __init__(self, master):
您在构造函数中没有任何内容,也许您应该在下面放置以下代码:
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()
也许您想在对象之后“清理”,那么我建议阅读更多有关此的信息,例如在这里: 如何正确清理Python对象?
还有你的另一种方法:
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
我几乎不建议您首先阅读有关python编程的基础知识,然后再开始使用一些高级模块。 我想在这里重定向您: https : //docs.python.org/3/tutorial/classes.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.