简体   繁体   中英

Where does this argument come from?

Edit: Apparently I'm not very clear. I really don't know what's happening, so I didn't know what to ask for specifically. My question is how nap gets its argument, while I didn't specify one. Inclement understood what I meant, so I guess this is answered now.

This might be stupid but I really don't understand where the "nap" argument for update (in "update") comes from. Edit: (Where it receives it's value from, is what I mean).

"Update" is only called from (edit: passed through) "on_start", nowhere else.

class ClockApp(App):
    sw_started = False
    sw_seconds = 0

    def on_start(self):
        Clock.schedule_interval(self.update, 0)

    def update(self, nap):
        if self.sw_started:
            self.sw_seconds += nap

        self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')

        m, s = divmod(self.sw_seconds, 60)
        self.root.ids.stopwatch.text = (u'{0:02d}:{1:02d}.[size=40]{2:02d}[/size]'
                                        .format(int(m), int(s), int(s * 100 % 100)))

    def start_stop(self):
        self.root.ids.start_stop.text = 'Start' if self.sw_started else 'Stop'
        self.sw_started = not self.sw_started

    def reset(self):
        if self.sw_started:
            self.root.ids.start_stop.text = 'Start'
            self.sw_started = False

        self.sw_seconds = 0

update is not directly called, but scheduled with the Clock via Clock.schedule_interval(self.update, 0) . This automatically passes an argument to the function corresponding to the time since it was last called, which is what is called nap in this example.

Since the time between calls is set to 0 , the function will be called every frame and nap should end up being about 1/60.

For comparison, if you changed it to Clock.schedule_interval(self.update, 1) , you would find that nap is always approximately 1...but not exactly 1, due to small (or if the main thread is blocked, large) fluctuations in when frames are pushed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM