我正在尝试为 Ubuntu 构建一个关机暂停程序,但总体而言,我仍然在努力掌握 Python。
PyGTK 甚至是基于的,因此我不太清楚如何进行时间/日期管理。有没有人有关于 PyGTK 中时间或日期甚至日历管理的资源。
我需要能够检查今天的日期并将其与用户想要的日期进行比较。任何帮助都将不胜感激。
以下是贪睡器背后的代码:
import gettext
from gettext import gettext as _
gettext.textdomain('snooze')
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):
print "CREATING CONFIG"
config.add_section('Preferences')
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)
rewriteConfigFile(filename)
# Writing our configuration file to the failename as specifeid
def rewriteConfigFile(filename):
with open(filename, 'wb') as configfile:
config.write(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):
createConfigFile(configFile)
readConfigFile(configFile)
else:
readConfigFile(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:
self.daymon.activate()
if tueState == True:
self.daytues.activate()
if wedState == True:
self.daywed.activate()
if thurState == True:
self.daythur.activate()
if friState == True:
self.dayfri.activate()
if satState == True:
self.daysat.activate()
if sunState == True:
self.daysun.activate()
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)
rewriteConfigFile(configFile)
self.statusBar.set_text("Saved Config")
答案1
你可能想要使用datetime
模块,更具体地说datetime.date
如果您只想要日期而不是时间,则需要类别。
>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2012, 11, 12)
>>> today.isoformat()
'2012-11-12'
>>>
编辑 您可以使用 GObject 计时器每 x 秒检查一次。如下所示(未经测试):
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)