2011-09-09 124 views
2

我剛剛學習Python,並且作爲我一直致力於鬧鐘的朋友向我推薦的基本挑戰之一。我成功製作了一個在預定時間播放.wav聲音的鬧鐘。現在我一直在使用Pygame進行圖形用戶界面,這一切都很好,直到我必須設置按鈕來調整鬧鐘時間。看看我比較鬧鐘時間和時鐘時間,時鐘時間是字符串形式,所以鬧鐘時間也一樣。但按鈕無法+或 - 從字符串,所以我有點卡住了。我嘗試了將它變成一個字符串的方法,但到目前爲止,所有的一切都沒有成功。想知道這裏有人有沒有建議。調整鬧鐘時間以匹配python中的時鐘時間

下面的代碼:

#!/usr/bin/python 
import os.path, sys, datetime, time 
import os, sys, math 
import pygame, random 
from pygame.locals import * 

main_dir = os.path.split(os.path.abspath(__file__))[0] 
data_dir = os.path.join(main_dir, 'data') 
currenttime = datetime.datetime.now() 
clocktime = currenttime.strftime("%H:%M") 
alarmtime = "13:23" 
pygame.init() 

#Screen and background 
width, height = 600, 600 
screen = pygame.display.set_mode((width, height)) 
pygame.display.set_caption("Alarm Clock") 
background = pygame.image.load(os.path.join(data_dir, 'diamondplate.jpg')) 
background = pygame.transform.scale(background, (width, height)) 

#Current time 
font = pygame.font.Font(None, 250) 
text = font.render("%s" % clocktime, True, (255,140,0), (0,0,0)) 
textRect = text.get_rect() 
textRect.centerx = screen.get_rect().centerx 
textRect.centery = screen.get_rect().centery - 200 

#Alarm time 
text2 = font.render("%s" % '00:00', True, (255,140,0), (0,0,0)) 
text2Rect = text2.get_rect() 
text2Rect.centerx = screen.get_rect().centerx 
text2Rect.centery = screen.get_rect().centery + 200 

#Alarm noise 
def alarmsound(file_path=os.path.join(main_dir, 'data', 'boom.wav')): 
    pygame.mixer.init(11025) 
    sound = pygame.mixer.Sound(file_path) 
    channel = sound.play() 
    pygame.time.wait(1000) 

#Image load function 
def load_image(file): 
    file = os.path.join(data_dir, file) 
    surface = pygame.image.load(file) 
    return surface.convert_alpha() 

#Hour arrow up 
class Hourup(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) 
     image = load_image('arrowup.png') 
     image = pygame.transform.scale(image, (85,85)) 
     self.image = image 
     self.rect = self.image.get_rect() 
     surface = pygame.display.get_surface() 
     self.area = surface.get_rect() 
     self.rect.bottomleft = text2Rect.topleft 

    def click_check(self,eventpos): 
     if self.rect.collidepoint(eventpos): 
      pass 

    def update(self): 
     pass 

#Hour arrow down 
class Hourdown(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) 
     image = load_image('arrowdown.png') 
     image = pygame.transform.scale(image, (85,85)) 
     self.image = image 
     self.rect = self.image.get_rect() 
     surface = pygame.display.get_surface() 
     self.area = surface.get_rect() 
     self.rect.bottom = text2Rect.top 
     self.rect.left = 159 

    def click_check(self,eventpos): 
     if self.rect.collidepoint(eventpos): 
      pass  

    def update(self): 
     pass 

#Minute arrow up 
class Minuteup(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) 
     image = load_image('arrowup.png') 
     image = pygame.transform.scale(image, (85,85)) 
     self.image = image 
     self.rect = self.image.get_rect() 
     surface = pygame.display.get_surface() 
     self.area = surface.get_rect() 
     self.rect.bottomright = (442,414) 

    def click_check(self,eventpos): 
     if self.rect.collidepoint(eventpos): 
      pass 

    def update(self): 
     pass 

#Minute arrow down 
class Minutedown(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self,self.groups) 
     image = load_image('arrowdown.png') 
     image = pygame.transform.scale(image, (85,85)) 
     self.image = image 
     self.rect = self.image.get_rect() 
     surface = pygame.display.get_surface() 
     self.area = surface.get_rect() 
     self.rect.bottomright = text2Rect.topright 

    def click_check(self,eventpos): 
     if self.rect.collidepoint(eventpos): 
      pass 

    def update(self): 
     pass 


#Groups 
allsprites = pygame.sprite.Group() 
Hourup.groups = allsprites 
Hourdown.groups = allsprites 
Minutedown.groups = allsprites 
Minuteup.groups = allsprites 
hourup = Hourup() 
hourdown = Hourdown() 
minutedown = Minutedown() 
minuteup = Minuteup() 
clickableobjects = [hourup, hourdown, minutedown, minuteup] 

def main(): 
    while 1: 
     currenttime = datetime.datetime.now() 
     clocktime = currenttime.strftime("%H:%M") 
     screen.blit(background,(0,0)) 
     text = font.render("%s" % clocktime, True, (255,140,0), (0,0,0)) 
     text2 = font.render("%s" % alarmtime, True, (255,140,0), (0,0,0)) 
     screen.blit(text,textRect) 
     screen.blit(text2,text2Rect) 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): 
       sys.exit() 
      if event.type == MOUSEBUTTONDOWN: 
       if event.button == 1: 
        for object in clickableobjects: 
         object.click_check(event.pos) 

     if clocktime == alarmtime and soundcheck = False: 
      alarmsound() 
      soundcheck = True 
     allsprites.draw(screen) 
     allsprites.update() 
     pygame.display.update() 
     pygame.display.flip 

if __name__ == '__main__': 
    main() 

回答

1

您正在尋找strptime()將一個字符串轉換爲datetime實例。

請參閱here瞭解如何正確使用它。

比較兩個日期時間實例會給你一個timedelta實例,你可以閱讀關於here的一個實例。本質上它會給你兩次到最接近的毫秒之間的差異。

瞭解有關日期時間,時間和日曆模塊的所有信息。一旦你瞭解到python中處理時間和日期的事情變得非常簡單。