0

I have a function that is run when a key is pressed, this is done using the following:

root.bind("<Key>", lambda event:KeyPressed(event, string_x, ...)

This function appends the letter associated with the key that was pressed to string_x. Once it's done this, I need it to return the new value of string_x so that it can be used in main.py. My file structure is as follows:

-> main.py
-> keypressed.py

I have tried parsing the function bound to the event another "callback" function, that should run a function in main.py, but it doesn't seem to do anything at all, and I'm not sure why.

Lordimass
  • 97
  • 2
  • 6
  • 1
    Can you show what exactly it was that you attempted? Because there's no way to get the return value of the callback function. – Matiiss Jul 07 '23 at 10:13
  • 1
    You can't "return a value" from this function. You can instead update a class or global variable, post a message to a queue, etc. – larsks Jul 07 '23 at 11:04

1 Answers1

1

How do I return values after running a function from binding a key?

You can't. When a bound function is called, it's not called by your code. It's called from the internals of mainloop. Anything returned by a bound function -- except for the special string "break" -- is ignored. Returning the string "break" will cause any further processing of that event to stop.

You will need to set a global variable if not using classes, or set an instance variable if using classes.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685