Search code examples
pythonpyjamas

Timer not being called by button press in pyjs


This is the main index class of my application which loads the various Classes in a DockPanel. I am attempting to have the button call a Timer function using it clicklistener ("OnClick"). The button loads the next screen, which is an image of a spinner that I want to be able to control how long it is displayed before loading the next screen (this is part of an experimental condition, I realize this is not a great way of making a loader). Problem is that the button press loads the next page, however it does not activate the Timer which causes the page to be displayed indefinitely.

Currently the Timer is being loaded into the SOUTH section of a DockPanel, however I have also tried simply calling the Timer function in "OnClick". (e.g. "Timer(2000, self.OnTimer)")

Does anyone have any idea why my Timer is not working and how to solve this?

EDIT: I have also tried to put it in the init function of the Project class, however this launches Timer as soon as the app loads. So after the time interval it just loads the target page without having pushed the button.

class Project(SimplePanel):
    def onModuleLoad(self):
        SimplePanel.__init__(self)

        self.panel= DockPanel()
        self.horizontalslide=HorizontalSliderPage()
        self.button=Button("CALCULATE")
        self.horizontalslide.add(self.button)
        self.button.addClickListener(getattr(self, "OnPress"))
        self.recommendation=Details()   
        self.spinner=Spinner()

        self.index=HTML("<h3>Welcome to this experiment.</h3>")
        self.curPage=self.index

        self.vp=VerticalPanel()
        self.vp.add(self.index)

        self.link1=Hyperlink("CONTINUE")
        self.link1.addClickListener(getattr(self, 'ONLINK1'))

        self.panel.add(self.link1, DockPanel.SOUTH)

        self.panel.add(self.index, DockPanel.CENTER)

        RootPanel().add(self.panel)


    def ONLINK1(self, sender):
        self.panel.remove(self.curPage)
        self.panel.remove(self.link1)
        self.panel.add(self.horizontalslide, DockPanel.CENTER)
        self.curPage=self.horizontalslide

    def OnPress(self, sender):
        self.panel.remove(self.curPage)
        self.panel.add(self.spinner, DockPanel.CENTER)
        self.curPage=self.spinner
        return CalculatePercentage()
        self.timer=Timer(2000, self.OnTimer)
        self.panel.add(self.timer, DockPanel.SOUTH)

    def OnTimer(self, target):
        self.panel.remove(self.curPage)
        self.panel.add(self.recommendation, DockPanel.CENTER)
        self.curPage=self.recommendation

Solution

  • I've found the solution. It seems that the return within the ClickListener was interfering with calling the Timer. This is how it looks now:

    def OnPress(self, sender):
        self.panel.remove(self.curPage)
        self.panel.add(self.spinner, DockPanel.CENTER)
        self.curPage=self.spinner
        Timer(5000, self.OnTimer)
        return CalculatePercentage()
    
    
    def OnTimer(self, target):
        self.panel.remove(self.curPage)
        self.panel.add(self.recommendation, DockPanel.CENTER)
        self.curPage=self.recommendation