3

Is there any way to pass a custom parameter (string for instance) through the event_generate() method?

The problem is that i have two threads. one of them should pass a variable which is required by event handler in the other one. I've tried to declare eventHandler with additional parameter e.g. def refreshCanvas(self, event, param) but then I am unable to pass param through event_generate().

I've also tried to pass it as an existing parameter 'state', which supposed to be a string, but it does not work either.

Class that generates an event:

class GameThread(Thread):
    def __init__(self, gameInstance, gui):
        Thread.__init__(self)

        self.__gameInstance = gameInstance
        gui.event_generate("<<refresh>>", when="tail", state=str(self.__gameInstance.getState()))

The class which suppose to catch this event:

class App(ttk.Frame):
    def __init__(self, parent=None):
        ttk.Frame.__init__(self, parent)
        parent.bind("<<refresh>>", self.refreshCanvas)

    def refreshCanvas(self, event):
        #something
        pass
1

1 Answer 1

2

I would suggest using the data option as http://www.tcl.tk/man/tcl/TkCmd/event.htm#M16 says

-data string String may be any value; it specifies the user_data field for the event. Only valid for virtual events. Corresponds to the %d substitution for virtual events in binding scripts.

However I've found that it's not yet in Python (see: http://bugs.python.org/issue3405) for now I would suggest use something like a queue and pass the data to the queue and then generate the event and pull the data from the queue in your handler method

EDIT:

A side note I would be careful about using any threading with Tkinter in general. I would suggest looking into using mtTkinter to be on the safe side. mtTkinter is a wrapper around Tkinter that makes it thread safe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.