Reputation:
def gate(i):
input.bind('<Return>', gate(input.get('end-1c linestart', 'end-1c lineend')))
inputDatabase.run(i)
input.bind('<Return>', gate(input.get('end-1c linestart', 'end-1c lineend')))
input
is a tkinter text object.
I am making a console, so that when the user presses the return key it gets the last line in the text object. This needs to happen forever.
Gives RecursionError: maximum recursion depth exceeded
. How can I make this work? I have tried using threads, but they are infinite, and because they are sharing the resource (I think) of the text object, they have to use locks, and I could not figure out how to do that. If anybody could explain how to use locks, threading or anything of the sort that would be extremely helpful. Thanks! Also two more things: do not suggest changing the system recursion depth please, and also I would prefer if the answer was in procedural rather than heavily object-orientated. It doesn't matter too much though. Again, thanks in advance.
Upvotes: 0
Views: 77
Reputation: 143097
As for me you need
def gate(event):
i = input.get('end-1c linestart', 'end-1c lineend')
inputDatabase.run(i)
# ---
input.bind('<Return>', gate)
EDIT: I forgot about event
in previous code (now it is added).
Tkinter executes binded gate
with argument which gives you access to widget so you can use it instead of input
def gate(event):
i = event.widget.get('end-1c linestart', 'end-1c lineend')
inputDatabase.run(i)
# ---
input.bind('<Return>', gate)
Upvotes: 0
Reputation: 386342
The problem is that you aren't doing the binding properly. In your bind statement you are immediately calling the function and then doing the same thing inside the function.
In other words, this:
input.bind('<Return>', gate(input.get('end-1c linestart', 'end-1c lineend')))
is exactly the same as this:
value = input.get('end-1c linestart', 'end-1c lineend')
input.bind('<Return>', value)
Since you are doing that inside the function, when the function is called it immediately calls itself again, which immediately calls itself again, which ... is why you get a recursion error.
You must pass a callable to the bind
method. It's a best practice to avoid lambda always have a bind call a function unless you need to pass arguments. When you bind the event properly, you only need to set the binding once for it to work for the life of the program.
For example:
def gate(event):
i = input.get('end-1c linestart', 'end-1c lineend')
inputDatabase.run(i)
input.bind('<Return>', gate)
Upvotes: 1