문제

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