我正在尝试使用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上的标签。但是,当我这样做时,标签不会更改,并且控制台输出似乎是两个空行的内容。 为什么我的函数没有设置“hello received”或“hello not received”的响应,如果是,为什么不打印或更新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()
将代码发布为纯文本,而不是截图。 – Barmar
正在更新 - 将离开控制台Image – BritishFerret
缩进是错误的,或者这是真正奇怪的使用类的方法(不包括'self's) – Lafexlos