pythontkinterfile-structure

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


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.


Solution

  • 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.