Commit f12d8d02 authored by Bruce Cowan's avatar Bruce Cowan

--no commit message

--no commit message
parents
from datetime import datetime
from gi.repository import GObject, Gtk, Pango
class AboutDialog(Gtk.AboutDialog):
"""An about dialog"""
__gtype_name__ = 'AboutDialog'
def __init__(self):
Gtk.AboutDialog.__init__(self)
self.set_authors(["Bruce Cowan <bruce@bcowan.me.uk>"])
self.set_comments("Countdown counts time down to a set time")
self.set_license_type(Gtk.License.MIT_X11)
self.set_modal(True)
self.set_program_name("Countdown")
\ No newline at end of file
from datetime import datetime, timedelta
import math
from gi.repository import GObject, Gtk, Pango
class Countdown(Gtk.Label):
"""A label that displays a countdown to a specific time"""
__gtype_name__ = 'Countdown'
def __init__(self):
Gtk.Label.__init__(self)
self.target = datetime.now()
fd = Pango.FontDescription.from_string("Monospace 36")
self.override_font(fd)
self._update_clock()
GObject.timeout_add_seconds(1, self._update_clock)
def _update_clock(self):
time = datetime.now()
delta = self.target - time
text = self._format_delta(delta)
self.set_label(text)
return True
def _format_delta(self, delta):
span = abs(math.ceil(delta.total_seconds()))
seconds = int(span % 60)
span /= 60
minutes = int(span % 60)
span /= 60
hours = int(span % 24)
span /= 24
days = int(span)
return "{:3d} days\n{:3d} hours\n{:3d} minutes\n{:3d} seconds".format(days, hours, minutes, seconds)
\ No newline at end of file
#!/usr/bin/env python
from datetime import datetime
import sys
from gi.repository import GLib, Gio, Gtk
from digdate import DigitalClock
from countdown import Countdown
from timechooserdialog import TimeChooserDialog
from aboutdialog import AboutDialog
class CountdownApplication(Gtk.Application):
__gtype_name__ = 'CountdownApplication'
def __init__(self):
Gtk.Application.__init__(self, application_id="uk.me.bcowan.countdown")
def do_activate(self):
self.win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
try:
builder = Gtk.Builder()
builder.add_from_file("menu.ui")
self.set_app_menu(builder.get_object("app_menu"))
except:
print("Something exploded")
time_action = Gio.SimpleAction(name="choose_time")
time_action.connect("activate", self._on_choose_time)
self.add_action(time_action)
about_action = Gio.SimpleAction(name="about")
about_action.connect("activate", self._on_about)
self.add_action(about_action)
quit_action = Gio.SimpleAction(name="quit")
quit_action.connect("activate", self._on_quit)
self.add_action(quit_action)
self._buildUI()
def _buildUI(self):
self.win = Gtk.ApplicationWindow(title="Countdown", resizable=False, application=self)
grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.win.add(grid)
clock = DigitalClock()
grid.add(clock)
self.count = Countdown()
grid.add(self.count)
def _on_choose_response(self, dialogue, resp_id):
self.count.target = dialogue.get_datetime()
dialogue.destroy();
def _on_choose_time(self, action, param):
dialogue = TimeChooserDialog(self.count.target)
dialogue.set_transient_for(self.win)
dialogue.show_all()
dialogue.connect("response", self._on_choose_response)
# Turn this into a lambda if possible
def _on_about_response(self, dialogue, resp_id):
dialogue.destroy();
def _on_about(self, action, param):
dialogue = AboutDialog()
dialogue.set_transient_for(self.win)
dialogue.show_all()
dialogue.connect("response", self._on_about_response)
def _on_quit(self, action, param):
self.quit()
if __name__ == "__main__":
app = CountdownApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
\ No newline at end of file
from datetime import datetime
from gi.repository import GObject, Gtk, Pango
class DigitalClock(Gtk.Label):
"""A label which shows the current time"""
__gtype_name__ = 'DigitalClock'
def __init__(self):
Gtk.Label.__init__(self)
fd = Pango.FontDescription.from_string("Monospace 48")
self.override_font(fd)
self._update_clock()
GObject.timeout_add_seconds(1, self._update_clock)
def _update_clock(self):
time = datetime.now()
text = time.strftime("%d/%m/%y %H:%M:%S")
self.set_label(text)
return True
\ No newline at end of file
<?xml version="1.0"?>
<interface>
<menu id="app_menu">
<section>
<item>
<attribute name="action">app.choose_time</attribute>
<attribute name="label">Choose time</attribute>
</item>
</section>
<section>
<item>
<attribute name="action">app.about</attribute>
<attribute name="label">About</attribute>
</item>
</section>
<section>
<item>
<attribute name="action">app.quit</attribute>
<attribute name="label">Quit</attribute>
<attribute name="accel">&lt;Primary&gt;q</attribute>
</item>
</section>
</menu>
</interface>
\ No newline at end of file
from datetime import datetime
from gi.repository import Gtk
class TimeChooserDialog(Gtk.Dialog):
"""A dialogue for choosing a time"""
__gtype_name__ = 'TimeChooserDialog'
def __init__(self, target):
Gtk.Dialog.__init__(self, title="Choose a time", flags=Gtk.DialogFlags.MODAL, resizable=False)
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
self.add_button(Gtk.STOCK_APPLY, Gtk.ResponseType.ACCEPT)
grid = Gtk.Grid()
self.get_content_area().add(grid)
# [H]:[M]:[S] part
self.hour = Gtk.SpinButton.new_with_range(0, 23, 1)
self.hour.set_orientation(Gtk.Orientation.VERTICAL);
self.hour.set_value(target.hour)
self.hour.connect("output", self._on_spin_output)
grid.add(self.hour)
label = Gtk.Label(label=":")
grid.add(label)
self.minute = Gtk.SpinButton.new_with_range(0, 59, 1)
self.minute.set_orientation(Gtk.Orientation.VERTICAL);
self.minute.set_value(target.minute)
self.minute.connect("output", self._on_spin_output)
grid.add(self.minute)
label = Gtk.Label(label=":")
grid.add(label)
self.second = Gtk.SpinButton.new_with_range(0, 59, 1)
self.second.set_orientation(Gtk.Orientation.VERTICAL);
self.second.set_value(target.second)
self.second.connect("output", self._on_spin_output)
grid.add(self.second)
# Calendar
self.calendar = Gtk.Calendar()
self.calendar.select_day(target.day)
self.calendar.select_month(target.month - 1, target.year)
grid.attach_next_to(self.calendar, self.hour, Gtk.PositionType.TOP, 5, 1)
def _on_spin_output(self, spin):
value = spin.get_value_as_int()
text = "{:02d}".format(value)
spin.set_text(text)
return True
def get_datetime(self):
(year, month, day) = self.calendar.get_date()
month += 1 # For some obscure reason, month is from 0-11
hour = self.hour.get_value_as_int()
minute = self.minute.get_value_as_int()
second = self.second.get_value_as_int()
return datetime(year, month, day, hour, minute, second)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment