例子:
利用qtdesigner建立闹钟
python代码
# 导入所需要的文件
from PyQt6.QtGui import QIcon, QPixmap
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QListWidgetItem
from PyQt6 import uic
from PyQt6.QtCore import Qt, QTime
import sys# 创建类继承
class Window(QMainWindow):def __init__(self):super().__init__()# 将ui文件中的内容导入ui=uic.loadUi("./闹钟.ui",self)self.resize(1000,618)self.t1 = self.startTimer(1000)self.pushButton.clicked.connect(self.start_clock)self.time_list=[]self.hh.setMaximum(24)self.hh.setMinimum(0)self.mm.setMaximum(59)self.mm.setMinimum(0)self.ss.setMaximum(59)self.ss.setMinimum(0)self.frame.hide()self.pushButton_2.clicked.connect(self.change)self.flag_change=True#将绑定的切换函数,使得两个类型可以相互切换def change(self):if self.flag_change:self.frame.show()self.frame_2.hide()self.flag_change=Falseelse:self.frame.hide()self.frame_2.show()self.flag_change = Truedef start_clock(self):#将读取到的时间转为字符self.time1 = self.lineEdit.text()self.time2 = str(self.hh.value())+":"+str(self.mm.value())+":"+str(self.ss.value())#将设定的时间存储到列表中,并且输出到linewidget上if self.time1=="" and self.time2 != "":self.time_list.append(self.time2)item = QListWidgetItem(self.time2)item.setTextAlignment(Qt.AlignmentFlag.AlignRight)self.listWidget.addItem(item)elif self.time1 != "" and self.time2 == "":self.time_list.append(self.time1)item = QListWidgetItem(self.time1)item.setTextAlignment(Qt.AlignmentFlag.AlignRight)self.listWidget.addItem(item)def timerEvent(self, a0):if a0.timerId()==self.t1:sys_time=QTime.currentTime()self.str_time = sys_time.toString("hh:mm:ss")self.str_time_show="当前时间:"+self.str_timeself.label.setText(self.str_time_show)#判断时间是否在闹钟列表中,如果存在则输出时间到了for i in self.time_list:if i == self.str_time:self.listWidget.addItem("时间差不多喽")self.time_list.remove(i)#程序入口
if __name__=="__main__":app=QApplication(sys.argv)win=Window()win.show()sys.exit(app.exec())