2016-09-06 12 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가 업데이트되지 않는 이유는 무엇입니까?

Py-Var2를 인쇄 (AI_RESPONSE)하면 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

업데이트 중 - 콘솔에서 나갈 것임 이미지 – BritishFerret

+1

들여 쓰기가 잘못되었거나 클래스를 사용하는 것이 정말 이상한 방법입니다 (자체 없이는) – 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 그래서 대신 =

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

.set(text)를 사용해야합니다 그리고 지금은 값을 반환 할 필요가 없습니다, global을 사용할 필요가 없습니다. 라벨과 콘솔에 텍스트가 표시됩니다.

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

다음 방법

class App: 
    def __init__(self, master): 
당신은 생성자에서 아무것도 필요 없다

는, 어쩌면 당신이 거기에 코드 아래에 놓아야합니다 : 처음부터 좋아

0

, 난 당신이 코드에서 몇 가지 실수가 있다고 생각 :

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 

내가 거의 당신을 추천하지 처음에는 모든보다는 몇 가지 고급 모듈을 사용하기 시작 파이썬 프로그래밍의 기본 사항에 대해 읽어보십시오. 여기로 리디렉션하고 싶습니다. https://docs.python.org/3/tutorial/classes.html

+0

엄격하게 질문에 답하지 않고 (내가 물어 본 것이 있습니다) 팁과 방향 전환에 감사드립니다. 나는이 모든 것들을 확실히 살펴볼 것입니다. 고맙습니다! – BritishFerret

관련 문제