In my View class I want to use pydispatcher.dispatcher send method in a tkinter Button command to signal to my Controller class in a MVC design that the button has been pressed. I do this:

self.calc_button = tkinter.Button(self.bottom_frame,text='Convert', \
        command=dispatcher.send(signal = 'valueToConvert', sender=dispatcher.Any, message='Kilo'))

The weird thing (to me) is that the dispatcher.send is “activated” when the button is created. Why? Further the dispatcher.send is never “activated” when the button is pressed. Why?

In the controller class this is the “listener” construction:

class Controller():

    def __init__(self):
        dispatcher.connect(self.myListener, 
                        signal='valueToConvert',
                        sender=dispatcher.Any)
        self.model = Model()
        self.view = View()
    def myListener(self, message, arg2=None):
        print("Received the following message: " + message)
有帮助吗?

解决方案

I don't know anything about dispatchers, but this is a common problem: you're assigning the result of the function call to command because you're not wrapping it in a lambda. Try:

self.calc_button = tkinter.Button(self.bottom_frame,text='Convert', \
    command=lambda *args: dispatcher.send(signal = 'valueToConvert', sender=dispatcher.Any, message='Kilo'))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top