2016-09-06 117 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

相關問題