0

I'm trying to build an AI with tkinter, but i've one problem. I've done a test but the it seems like the input_get variable doesn't change. When I call the variable in the function it work but if I call it outside it doesn't. Have you some advices ? (It's just a part of the code, the frame, the input field (Entry),etc are already put)

from tkinter import *
from tkinter import font

ia_answers = ""
input_get = ""

window = Tk()

window.config(cursor="wait")

input_frame = LabelFrame(window, text="User :", borderwidth=4)
input_frame.pack(fill=BOTH, side=BOTTOM)

input_user = StringVar()
input_field = Entry(input_frame, text=input_user)
input_field.pack(fill=BOTH, side=BOTTOM)

ia_frame = LabelFrame(window, text="Discussion",borderwidth = 15, height = 200, width = 200)
ia_frame.pack(fill=BOTH, side=TOP, expand = True)

printopt = font.Font(family = "Times")

text = Text(ia_frame, state='disabled', bg ="grey")
text.pack(fill = BOTH, expand = True, side = "left")
text.tag_configure("right", justify="right", font=printopt)
text.tag_configure("left", justify="left", font=printopt)
scr = Scrollbar(ia_frame)
scr.config(command=text.yview)
text.config(yscrollcommand=scr.set)
scr.pack(side="right", fill="y", expand=False)


def Enter_pressed(event):
    """Took the current string in the Entry field."""
    global input_get
    input_get = input_field.get()
    input_user.set("")
    text.configure(state='normal')
    text.insert("end", "\n"+input_get+"\n", "left")
    text.insert("end", "\n"+ia_answers+"\n", "right")
    text.configure(state='disabled')
    text.yview(END)
    return input_get

def inputget(event):
 if input_get == "ok":
    ia_answers = "test"

input_field.bind("<Return>", Enter_pressed, inputget)


window.mainloop()

Thank you. Ilan Rossler

Ilan Rossler
  • 33
  • 1
  • 12

1 Answers1

0

Two problems:

First, the if input_get == "ok" is being executed exactly once, when you first run the program. If you want it to run at certain times, put it in an event-driven function.

Second, input_get is assigned in the Enter_pressed function. When that function ends, all of its local variables fall out of scope and are discarded - including input_get. Since you can't return something to a Button widget or a keypress, if you want to use it in a different function or other scope, you'll have to save it as such. You can do this by initializing the variable before the function runs (e.g. input_get = '', then, in the function, declare global input_get. This will indicate to the interpreter that you intend to assign values to this global variable. Without this declaration, you can read the value of global variables, but reassigning to them will simply assign to a new, local reference of the same name, which you don't want. However, the better option is to go to an object-oriented approach and save it as an instance variable, self.input_get (see here and here).

Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I had initialize the variables at the beginning of the script but it wasn't working, I will try the event driven function advice – Ilan Rossler Aug 12 '15 at 06:49
  • If you initialize somewhere, you have to combine that with a `global input_get` within the function so that you can reassign to it. If you only check the value of `input_get` once, at the start of the program, it won't be checked anymore unless you tell it to. – TigerhawkT3 Aug 12 '15 at 06:52
  • To my knowledge, doing something like `input_field.bind("", Enter_pressed, inputget)` binds that key to `Enter_pressed` and passes that function an argument of the function `inputget`. That doesn't make sense for your functions. You're also trying to `return` a value to the Return key, which, as mentioned in this answer, doesn't do anything. I recommend looking over the examples at [Effbot](http://effbot.org/tkinterbook/). – TigerhawkT3 Aug 12 '15 at 16:52