import gi import time import datetime import threading gi.require_version("Gtk", "3.0") from gi.repository import Gtk, GLib class Main: def __init__(self): self.timer = None self.event = None self.timer_running = False self.builder = Gtk.Builder() self.builder.add_from_file('pom.glade') #self.builder.add_from_file('secondtry.glade') self.builder.connect_signals(self) # button = self.builder.get_object("button") # button.connect("clicked", self.printText) window = self.builder.get_object("Main") window.connect('delete-event', Gtk.main_quit) window.show() self.time_display = self.builder.get_object("time_display") self.mode_switch = self.builder.get_object('modeSwitch') self.stack = self.builder.get_object("mainStack") self.mode_switch.connect('notify::active', self.work_break_switch) self.POMTIME = 25 * 60 self.BREAKTIME = 5 * 60 self.LONGBREAK = 30 * 60 self.time_left = self.POMTIME def updateDisplay(self,time_left): self.clock = str(datetime.timedelta(seconds = self.time_left)) self.time_display.set_text(self.clock) def countdown(self): #self.time_left future = datetime.datetime.now() + datetime.timedelta(seconds=self.time_left) if self.timer_running: while not self.event.is_set(): self.updateDisplay(self.time_left) self.time_left -= 1 time.sleep(1) if datetime.datetime.now() >= future: if self.mode_switch.get_active(): self.time_left = self.POMTIME print('swith on') self.mode_switch.set_active(False) else: self.time_left = self.BREAKTIME print('swith off') self.mode_switch.set_active(True) # add alarm or whatev here #can add an auto continue? self.pauseCountdown('') def work_break_switch(self, switch, active): if self.mode_switch.get_active(): state = "Pomtime" self.time_left = self.POMTIME else: state = "Breaktime" self.time_left = self.BREAKTIME print("Switch was turned to", state) self.updateDisplay(self.time_left) self.pauseCountdown('') def startCountdown(self,button): print('Pressed start') if not self.timer_running: print('Started timer') self.timer_running = True self.timer = threading.Thread(target=self.countdown) self.event = threading.Event() self.timer.daemon=True self.timer.start() def pauseCountdown(self,button): print('Pressed stop') self.event.set() self.timer_running = False def restartCountdown(self, button): print('Pressed restart') self.time_left = self.POMTIME self.work_break_switch(self.mode_switch,'') self.event.set() self.startCountdown(button) # Settings windows def openSettings(self, button): current_page = self.stack.get_visible_child() settingspage = self.stack.get_child_by_name('settingsView') if current_page is settingspage: self.closeSettings(button) else: self.stack.set_visible_child(settingspage) def closeSettings(self, button): mainPage = self.stack.get_child_by_name('timerView') self.stack.set_visible_child(mainPage) if __name__ == '__main__': main = Main() Gtk.main()