Python 为什么不是';我的函数没有返回任何东西吗?
我试图使用python和tkinter制作一个基本的聊天机器人,但遇到了一个问题。为了简单起见,我排除了tkinter代码。整个代码在底部可见Python 为什么不是';我的函数没有返回任何东西吗?,python,tkinter,Python,Tkinter,我试图使用python和tkinter制作一个基本的聊天机器人,但遇到了一个问题。为了简单起见,我排除了tkinter代码。整个代码在底部可见 def communicate(): sent.set(HUMAN_ENTRY.get()) bottalk(response) AI_RESPONSE.set(response.get()) print (response.get())
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”或“hello not received”,并更新GUI上的标签。但是,当我这样做时,标签并没有改变,控制台输出的似乎是两个空行。为什么我的函数没有将响应设置为“hello received”或“hello not received”,如果是,为什么它没有打印或更新GUI
导致Py-Var2的打印(AI_响应)显示输出了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()
由于响应作为值返回,因此它不会更新
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
。您可以在标签和控制台中看到文本。响应是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()
可能您想在对象之后“清理”,那么我建议您阅读更多关于此的内容,例如:
此外,您还可以使用其他方法:
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编程的基础知识,然后开始使用一些高级模块。我想将您重定向到这里:好的,从一开始,我认为您的代码中几乎没有错误:
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()
可能您想在对象之后“清理”,那么我建议您阅读更多关于此的内容,例如:
此外,您还可以使用其他方法:
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编程的基础知识,然后开始使用一些高级模块。我想在这里重定向您:将代码以纯文本形式发布,而不是屏幕截图。更新-将留下控制台图像缩进错误或这是使用类的非常奇怪的方式(没有self
s)Tkinter非常新。这是我第一次这么做,而且我必须遵循一个例子。很可能是完全不正确的。将代码以纯文本形式发布,而不是屏幕截图。更新-将离开控制台图像缩进错误或这是使用类的非常奇怪的方式(没有self
s)对于Tkinter来说非常非常新。这是我第一次这么做,而且我必须遵循一个例子。很可能完全不正确。这会导致错误:“UnboundLocalError:赋值前引用的局部变量'response'”我假设我需要全局设置变量,但在tkinter中哪里是可行的?这会导致错误:“UnboundLocalError:赋值前引用的局部变量'response'”我假设我需要全局设置变量,但在tkinter中哪里是可行的?不是严格地回答这个问题(我已经得到了我所问的),但是我非常感谢这些提示和重定向。我一定会研究所有这些问题。非常感谢。没有严格地回答这个问题(我已经记下了我的问题),但我非常感谢这些提示和重定向。我一定会研究所有这些问题。非常感谢。