Skip to main content
 首页 » 编程设计

python之调整闹钟时间以匹配python中的时钟时间

2025年05月04日7daizhj

我刚开始学习 Python,作为 friend 向我建议的基本挑战之一,我一直在研究闹钟。我成功地制作了一个在预定时间播放 .wav 声音的闹钟。现在我一直在使用 Pygame 作为 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() 

请您参考如下方法:

您正在寻找将字符串转换为日期时间实例的 strptime()。

参见 here了解如何正确使用它。

比较两个 datetime 实例会给你一个 timedelta 实例,你可以阅读关于 here 的内容.本质上,它会为您提供两个时间之间的差异,精确到毫秒。

了解有关日期时间、时间和日历模块的所有信息。一旦您学会了在 Python 中处理时间和日期,就会变得非常容易。