Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os.path
- from kivy.core.window import Window
- from kivy.properties import NumericProperty
- from kivy.clock import Clock
- from kivymd.app import MDApp
- from kivymd.uix.card import MDCard
- Window.size = (428, 926)
- class TimerCard(MDCard):
- # set variables to be used
- event = None
- targettime = NumericProperty(0)
- currenttime = NumericProperty(0)
- progressbaropacity = NumericProperty(0)
- startbuttonopacity = NumericProperty(100)
- # update the progressbar, reset when finished and add to score
- def update_bar(self, dt):
- if self.currenttime < self.targettime - 1:
- self.currenttime += 1
- else:
- self.event.cancel()
- self.event = None
- self.progressbaropacity = 0
- self.startbuttonopacity = 100
- self.currenttime = 0
- self.app.score += self.targettime * 1.1
- print(self.currenttime)
- # start updating the progress bar, repeat the update function once every second
- def start_update(self):
- if self.event is None and self.app.score >= self.targettime:
- self.event = Clock.schedule_interval(self.update_bar, 1)
- self.startbuttonopacity = 0
- self.progressbaropacity = 100
- self.app.score -= self.targettime
- class ProgressBarsApp(MDApp):
- # score variable
- score = NumericProperty(1)
- # load the savefile to resume progress. autosave every second
- def __init__(self, **kwargs):
- self.cards = []
- super().__init__()
- Clock.schedule_once(self.load, 0)
- Clock.schedule_interval(self.save, 1)
- # function for adding another card
- def make_card(self):
- card = TimerCard()
- card.app = self
- self.cards.append(card)
- self.root.ids.box.add_widget(card)
- # save to file function
- def save(self, _):
- with open("savefile.txt", mode="w") as f:
- savetext = str(self.score) + "\n"
- for card in self.cards:
- savetext += str(card.currenttime) + "," + str(card.targettime) + "\n"
- f.write(savetext)
- # load savefile function
- def load(self, _):
- if os.path.exists("savefile.txt"):
- with open("savefile.txt", mode="r") as f:
- self.score = float(f.readline())
- self.cards = []
- for line in f:
- current, target = line.split(",")
- self.make_card()
- self.cards[-1].currenttime = current
- self.cards[-1].targettime = target
- if float(current) > 0:
- self.score += float((target.strip()))
- self.cards[-1].start_update()
- if __name__ == "__main__":
- ProgressBarsApp().run()
Add Comment
Please, Sign In to add comment