Search code examples
pythoncallbackbeewareconfirm-dialog

Beeware Toga confirm_dialog callback error


I am trying to use the confirm_dialog window with Beeware. I understand that the on_result callback should be activated only when I click the OK button. Below i attach a simple script I use to try it. The confirm_dialog window pops-up ok, but when i click either the OK or the cancel button i get the error func2() takes 1 positional argument but 3 were given. I tried both in dev mode and in run android, and got the same issue. It looks to me that the problem is in the callback, but I could not find a way of making it work. I would much appreciate any answer or hint on what I am doing wrong.

UPDATE: I corrected the code below according to the answer by gimix that solved the problem. Now the code works just fine.

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
class HelloWorld(toga.App):

    def startup(self):   
        main_box = toga.Box(style=Pack(direction=COLUMN))
        record_button = toga.Button("func1",on_press=self.func1,style=Pack(padding=5))
        main_box.add(record_button)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box

    def func2(self,dialog,result):
        print("callback worked")

    def func1(self,widget):
        self.main_window.confirm_dialog("title", "message",on_result=self.func2)
        return 

def main():
    return HelloWorld()

Solution

  • I'm not a Toga expert, but I had a look at the definition of ConfirmDialog and its superclass MessageDialog, where your callback gets called:

            # def completion_handler(self, return_value: bool) -> None:
            if self.on_result:
                self.on_result(self, result)
    

    As you can see func2 is called with 3 arguments: self (your class), the self of the dialog (i.e. the dialog object), and finally the return value of the dialog. So the definition should be something like

    def func2(self, dialog, result):
        #do something