当前位置: 首页 > 后端技术 > Python

制作一个小闹钟,按计划做事...

时间:2023-03-26 01:12:14 Python

通过PyQt5实现设置小闹钟的功能,到了设定的时间会响起一段音乐提醒你。[阅读全文]需要小闹钟完整源码,文末获取下载链接。导入UI界面组件相关模块fromPyQt5.QtCoreimport*fromPyQt5.QtWidgetsimport*fromPyQt5.QtGuiimport*导入应用程序操作相关模块importsysfromPyQt5.QtMultimediaimport*初始化函数init_ui()函数,PyQt5界面布局使用三类型有垂直布局、水平布局和网格布局。definit_ui(self):self.setWindowTitle("Smallalarmclock")#设置应用程序标题self.setWindowIcon(QIcon('clock.ico'))#设置应用程序图标form=QFormLayout()#初始化一个表单布局self.current_date_label=QLabel()self.current_date_label.setText("当前时间:")self.current_date_label_time=QLabel()self.current_date_label_time.setText(QDateTime.currentDateTime().toString('yyyy-MM-ddhh:mm:ssdddd'))self.current_timer=QTimer()self.current_timer.timeout.connect(self.show_current)self.current_timer.start(1000)self.timing_date_label=QLabel()self.timing_date_label.setText("计时时间:")self.timing_date_time=QDateTimeEdit()self.timing_date_time.setDisplayFormat("yyyy-MM-ddHH:mm:ss")self.timing_date_time.setDateTime(QDateTime.currentDateTime())self.set_rightone_label=QLabel()self.set_rightone_label.setText("设置铃声:")self.set_rightone_box=QComboBox()self.set_rightone_box.addItems(["冷漠-一路向北(DJ版)","大城-雪域哈尔滨","许巍-时光"])form.addRow(self.current_date_label,self.current_date_label_time)form.addRow(self.timing_date_label,self.timing_date_time)form.addRow(self.set_rightone_label,self.set_rightone_box)hbox=QHBoxLayout()#初始化水平布局self.version=QLabel()self.version.setText("公众号:[Python集中营]")self.start_btn=QPushButton()self.start_btn.setText("Start")self.start_btn.clicked.connect(self.start_btn_click)hbox.addWidget(self.version)hbox.addWidget(self.start_btn)vbox=QVBoxLayout()#初始化垂直布局vbox.addLayout(form)vbox.addLayout(hbox)self.setLayout(vbox)#设置主布局创建槽函数show_current(),用于显示实时改变时间,将时间更新到QLabel组件上面,目前做的是秒级时间更新defshow_current(self):'''刷新当前时间显示,每秒刷新一次:return:'''current_time=QDateTime.currentDateTime().toString('yyyy-MM-ddhh:mm:ssdddd')self.current_date_label_time.setText(current_time)创建一个槽函数timing_his()来监控计时时间是否到达。计时器到达时播放音乐。现在代码块中一共引入了三首歌曲。您可以根据自己的喜好添加自己喜欢的歌曲。deftiming_lis(self):ifQDateTime.currentDateTime()