我正在嘗試使用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