2016-09-06 171 views
0

我正在尝试使用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() 

The Console

+1

将代码发布为纯文本,而不是截图。 – Barmar

+0

正在更新 - 将离开控制台Image – BritishFerret

+1

缩进是错误的,或者这是真正奇怪的使用类的方法(不包括'self's) – Lafexlos

回答

3

由于响应返回值,也不会内部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() 
+0

这会导致一个错误: “UnboundLocalError:局部变量'响应'在赋值之前引用” 我假设我需要在全局范围内设置变量,但tkinter中的哪个部分是可行的? – BritishFerret

1

responseStringVar所以你必须使用.set(text)代替=

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

现在你不必返回值,并且不需要使用global。你可以在标签和控制台中看到文字。

0

从开始好吧,我想你已经在你的代码一些错误:

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

可能你想“清理”后的对象,然后我建议阅读更多关于此,例如这里:How do I correctly clean up a Python object?
还您另一种方法:

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

+0

不严格回答这个问题(我有问题),但我很欣赏提示和重定向。我一定会研究所有这些。谢谢! – BritishFerret

相关问题