Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to build a Shutdown Snoozer for Ubuntu, but I am still struggeling with Python in general.

PyGTK is even based, and I don't quite know how to do the time / date management because of this. Does anyone have any resources for time or date or even calander management in PyGTK.

I need to be able to check todays date and compare it to what the user wants. Any help would be appreaciated.

Here is the code behind the snoozer:

import gettext
from gettext import gettext as _

from gi.repository import Gtk # pylint: disable=E0611
import logging, time, datetime

logger = logging.getLogger('snooze')

import ConfigParser, os # We need to be able to store and recal settings

from snooze_lib import Window
from snooze.AboutSnoozeDialog import AboutSnoozeDialog
from snooze.PreferencesSnoozeDialog import PreferencesSnoozeDialog

#first try to read the config.cfg file
config = ConfigParser.RawConfigParser()
configFile = 'data/config.cfg'
monState = False
tueState = False
wedState = False
thurState = False
friState = False
satState = False
sunState = False

# Creating the Config file
def createConfigFile(fileName):
    config.set('Preferences', 'mon', False)
    config.set('Preferences', 'tues', False)
    config.set('Preferences', 'wed', False)
    config.set('Preferences', 'thur', False)
    config.set('Preferences', 'fri', False)
    config.set('Preferences', 'sat', False)
    config.set('Preferences', 'sun', False)

# Writing our configuration file to the failename as specifeid
def rewriteConfigFile(filename):    
    with open(filename, 'wb') as configfile:
# Reading the config file 
def readConfigFile(fileName):
    print "READING CONFIG"
    global monState, tueState, wedState, thurState, friState, satState, sunState
    monState = config.getboolean('Preferences', 'mon')
    tueState = config.getboolean('Preferences', 'tues')
    wedState = config.getboolean('Preferences', 'wed')
    thurState = config.getboolean('Preferences', 'thur')
    friState = config.getboolean('Preferences', 'fri')
    satState = config.getboolean('Preferences', 'sat')
    sunState = config.getboolean('Preferences', 'sun')

# If the config does not exist, create it then read it. Otherwise read it
if not config.read(configFile):    

# See snooze_lib.Window.py for more details about how this class works
class SnoozeWindow(Window):
    __gtype_name__ = "SnoozeWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(SnoozeWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutSnoozeDialog
        self.PreferencesDialog = PreferencesSnoozeDialog

        # Code for other initialization actions should be added here.
        self.daymon = self.builder.get_object("daymon")
        self.daytues = self.builder.get_object("daytues")
        self.daywed = self.builder.get_object("daywed")
        self.daythur = self.builder.get_object("daythur")
        self.dayfri = self.builder.get_object("dayfri")
        self.daysat = self.builder.get_object("daysat")
        self.daysun = self.builder.get_object("daysun")
        self.statusBar = self.builder.get_object("statusBar")
        self.count = 0

        # Set the values based on the config file        
        if monState == True:

        if tueState == True:

        if wedState == True:

        if thurState == True:

        if friState == True:

        if satState == True:

        if sunState == True:

        self.daymon.connect('notify::active', self.toggle_day, "mon")
        self.daytues.connect('notify::active', self.toggle_day, "tues")
        self.daywed.connect('notify::active', self.toggle_day, "wed")
        self.daythur.connect('notify::active', self.toggle_day, "thur")
        self.dayfri.connect('notify::active', self.toggle_day, "fri")
        self.daysat.connect('notify::active', self.toggle_day, "sat")
        self.daysun.connect('notify::active', self.toggle_day, "sun")

    # Toggle the setting and store the information in the config file
    def toggle_day(self, widget, active, day):
        state = widget.get_active()

        # Set the config option and update the config file
        global configFile        
        config.set('Preferences', day, state)        
        self.statusBar.set_text("Saved Config")        
share|improve this question
You will likely get better results asking these types of questions on stack overflow. This is not really Ubuntu specific, or even Linux specific. Stack overflow is for help with just these types of problems. –  coteyr Nov 17 '12 at 10:30
Hi Coteyr, thank you. But I actually asked it on askubuntu.com. They are in the network of tech tip sites :-) –  Rudi Strydom Nov 17 '12 at 12:36

1 Answer 1

up vote 1 down vote accepted

You'll probably want to use the datetime module, more specificly the datetime.date class if you just want the date, not times.

>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2012, 11, 12)
>>> today.isoformat()

Edit You could use a GObject timer to check every x seconds. Something like this (not tested):

import time
from gi.repository import GObject

def _check_time_timer(self):
    if time.time() >= USER_TIME:
        # Do your action here
        print "Time has been reached!"
        # Return False to stop timer
        return False
    # Return True to keep the timer going
    return True

# Check the user time every 4 seconds, change as needed
GObject.timeout_add_seconds(4, _check_time_timer)
share|improve this answer
Timo you the only one on here? You don't have any examples by any chance? The reason I ask is, because I did use the time.time() function as an example and set the statusbar to the timestamp, but the status bar does not update as it's not a loop based GUI but rather an event based GUI. Is there a place in which to place a function which will form part of the event handler? –  Rudi Strydom Nov 13 '12 at 6:44
Ah, like that. I didn't see any loop in your code snippet. It depends on what kind of loop you're using and the overall implementation. With some luck, adding a while Gtk.events_pending(): Gtk.main_iteration() inside your loop can be enough, if not, post a simplified example. –  Timo Nov 13 '12 at 11:21
Problem with adding this code while Gtk.events_pending(): it basically waits for this loop before even generating the GUI. Any idea how I can run a subloop / loop alongside the main loop instead of before?? –  Rudi Strydom Nov 13 '12 at 20:32
Basically, I need to be able to check whether a specific time of day has been reached whilst the application is running in the background without the user needing to click on a button to fire of a function. –  Rudi Strydom Nov 13 '12 at 21:07
You could use a GObject timer for this, which won't block the mainloop. See my edit. –  Timo Nov 14 '12 at 12:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.