2011-09-09 6 views
2

저는 방금 Python을 배우고 있으며, 친구들이 나에게 제안한 기본 과제 중 하나 인 알람 시계를 작업 중입니다. 미리 정해진 시간에 .wav 사운드를 재생하는 알람 시계를 만들었습니다. 이제는 GUI를 위해 파이 게임을 사용하고 있는데, 알람 시간을 조정하기 위해 버튼을 설정해야 할 때까지는 모두 훌륭했습니다. 알람 시간을 시계 시간과 비교할 때 시계 시간은 문자열 형식이므로 알람 시간도 있어야합니다. 그러나 버튼은 문자열에서 + 또는 -를 사용할 수 없으므로 다소 방해가됩니다. 문자열로 바꾸는 방법을 시도했지만 지금까지는 모든 것이 상당히 실패했습니다. 여기 누구든지 제안이 있다면 궁금합니다.파이썬의 시계 시간과 일치하도록 알람 시간을 조정하십시오.

#!/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() 찾고있는 날짜 인스턴스에 문자열을 변환합니다 :

여기에 코드입니다.

올바르게 사용하는 방법은 here을 참조하십시오.

두 datetime 인스턴스를 비교하면 here에 대해 읽을 수있는 timedelta 인스턴스가 제공됩니다. 근본적으로 그것은 가장 가까운 밀리 초로 두 시간의 차이를 줄 것입니다.

datetime, time 및 calendar 모듈에 대해 할 수있는 모든 것을 배우십시오. 일단 파이썬으로 시간과 날짜를 다루는 사람들이 쉽게 배우게됩니다.