1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| newAlarm=QPushButton('新建',widgetslist[3]) deleteAlarm=QPushButton('编辑',widgetslist[3]) alarmScrollArea=QScrollArea(widgetslist[3]) alarmScrollArea.move(10,30) alarmScrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) alarmScrollAreaWidget=QWidget() alarmScrollArea.setWidget(alarmScrollAreaWidget) alarmList=[] class Alarm(QGroupBox): def __init__(self,parent,timelength,title,music,repeat,description,running): super().__init__(parent) self.timelength=timelength self.titlestr=title self.music=music self.running=running self.repeat=repeat self.description=description def launch(self): self.resize(400,300) self.restTime=self.timelength self.timelabel=QLabel(formatTime(self.restTime,round=True),self) font=QFont('Arian',40,10) self.timelabel.setFont(font) self.descriptionLabel=QLabel(self) self.descriptionLabel.setWordWrap(True) self.descriptionLabel.setAlignment(Qt.AlignmentFlag.AlignTop|Qt.AlignmentFlag.AlignLeft) self.repetitionLabel=QLabel(self) self.startStopButton=QPushButton('关闭' if self.running else '开启',self) self.startStopButton.clicked.connect(self.startStop) self.editButton=None self.deleteButton=None self.mode=True def toggleMode(self): if self.mode: self.startStopButton.hide() self.editButton=QPushButton('编辑',self) self.editButton.clicked.connect(self.edit) self.editButton.show() self.deleteButton=QPushButton('删除',self) self.deleteButton.clicked.connect(self.deleteSelf) self.deleteButton.show() else: self.startStopButton.show() self.editButton.hide() self.editButton=None self.deleteButton.hide() self.deleteButton=None self.mode=not self.mode def updating(self): hours,minutes,seconds=list(map(int,time.strftime('%H %M %S').split())) if abs(self.timelength-(hours*3600+minutes*60+seconds))<0.0005 and self.running: self.showMessage() self.startStop() self.setTitle(self.titlestr) selfsize=self.size() buttonsize=self.startStopButton.size() self.timelabel.setText(formatTime(self.timelength,round=True)) labelsize=self.timelabel.size() self.timelabel.move(selfsize.width()/2-labelsize.width()/2,30)
self.descriptionLabel.setText(self.description) self.descriptionLabel.setFixedSize(selfsize.width()-10,200) labelsize=self.descriptionLabel.size() self.descriptionLabel.move(selfsize.width()/2-labelsize.width()/2,150) text=['星期一','星期二','星期三','星期四','星期五','星期六','星期日'] list1=[] for i in range(7): if self.repeat[i]: list1.append(text[i]) self.repetitionLabel.setText(','.join(list1)) self.repetitionLabel.setFixedSize(selfsize.width()-10,20) labelsize=self.repetitionLabel.size() self.repetitionLabel.move(selfsize.width()/2-labelsize.width()/2,100) if self.mode: self.startStopButton.move(selfsize.width()/2-buttonsize.width()/2,selfsize.height()-10-buttonsize.height()) else: self.editButton.move(selfsize.width()/2-5-buttonsize.width(),selfsize.height()-10-buttonsize.height()) self.deleteButton.move(selfsize.width()/2+5,selfsize.height()-10-buttonsize.height()) def startStop(self): if self.running: self.startStopButton.setText('开启') else: self.startStopButton.setText('关闭') self.running=not self.running def edit(self): editor=AlarmEdit(self.timelength,self.titlestr,self.music,self.repeat,self.description,self.running) editor.editData() self.timelength=editor.length self.restTime=self.timelength self.titlestr=editor.titlestr self.music=editor.music self.repeat=editor.repeat self.description=editor.description self.running=editor.running def showMessage(self): self.trayicon=QSystemTrayIcon() self.trayicon.setIcon(icon) self.trayicon.show() self.trayicon.showMessage('闹钟'+formatTime(self.timelength,round=True),self.description,icon) def deleteSelf(self): alarmList.remove(self) self.deleteLater() del self class AlarmEdit(QDialog): def __init__(self,length,title,music,repeat,description,running): super().__init__() self.confirmed=False self.length=length self.titlestr=title self.music=music self.repeat=repeat self.description=description self.running=running def editData(self): self.setWindowTitle('闹钟编辑') self.vlayout=QVBoxLayout() self.gridlayout=QGridLayout() self.hlayout=QHBoxLayout() self.gridlayout.setSpacing(10) self.label1=QLabel('时间') self.label2=QLabel('标题') self.label3=QLabel('音乐') self.label4=QLabel('描述') self.label5=QLabel('重复') self.content1=QHBoxLayout() self.content1_1=QSpinBox() self.content1_1.setMinimum(0) self.content1_1.setMaximum(23) self.content1_1.setValue(self.length//3600) self.content1_2=QLabel(':') self.content1_3=QSpinBox() self.content1_3.setMinimum(0) self.content1_3.setMaximum(59) self.content1_3.setValue((self.length%3600)//60) self.content1_4=QLabel(':') self.content1_5=QSpinBox() self.content1_5.setMinimum(0) self.content1_5.setMaximum(59) self.content1_5.setValue(self.length%60) self.content1.addWidget(self.content1_1) self.content1.addWidget(self.content1_2) self.content1.addWidget(self.content1_3) self.content1.addWidget(self.content1_4) self.content1.addWidget(self.content1_5) self.content2=QLineEdit() self.content2.setText(self.titlestr) self.content3=QComboBox() self.content3.addItems(musicNameList) self.content3.setCurrentIndex(self.music) self.content4=QLineEdit() self.content4.setText(self.description) self.content5=QVBoxLayout() text=['星期一','星期二','星期三','星期四','星期五','星期六','星期日'] self.content5s=[QCheckBox(text=i) for i in text] for i in self.content5s: self.content5.addWidget(i) p=self.content5s.index(i) i.setChecked(self.repeat[p]) self.gridlayout.addWidget(self.label1,0,0) self.gridlayout.addWidget(self.label2,1,0) self.gridlayout.addWidget(self.label3,2,0) self.gridlayout.addWidget(self.label4,3,0) self.gridlayout.addWidget(self.label5,4,0) self.gridlayout.addLayout(self.content1,0,1) self.gridlayout.addWidget(self.content2,1,1) self.gridlayout.addWidget(self.content3,2,1) self.gridlayout.addWidget(self.content4,3,1) self.gridlayout.addLayout(self.content5,4,1) self.confirm=QPushButton('确定') self.cancel=QPushButton('取消') self.confirm.clicked.connect(self.confirming) self.cancel.clicked.connect(self.close) self.hlayout.addWidget(self.confirm) self.hlayout.addWidget(self.cancel) self.vlayout.addLayout(self.gridlayout) self.vlayout.addLayout(self.hlayout) self.setLayout(self.vlayout) self.show() self.exec() def setRepeat(self,p): self.repeat[p]=not self.repeat[p] def confirming(self): self.confirmed=True self.length=self.content1_1.value()*3600+self.content1_3.value()*60+self.content1_5.value() self.titlestr=self.content2.text() self.music=self.content3.currentIndex() self.description=self.content4.text() self.repeat=[i.isChecked() for i in self.content5s] self.close() def onNewAlarm(): global alarmList alarmEdit=AlarmEdit(0,'未命名',0,[False,False,False,False,False,False,False],'',True) alarmEdit.editData() if alarmEdit.confirmed: alarm=Alarm(alarmScrollAreaWidget,alarmEdit.length,alarmEdit.titlestr,alarmEdit.music,alarmEdit.repeat,alarmEdit.description,alarmEdit.running) alarm.launch() alarm.show() alarmList.append(alarm) def onDeleteAlarm(): global alarmList for i in alarmList: i.toggleMode() newAlarm.clicked.connect(onNewAlarm) deleteAlarm.clicked.connect(onDeleteAlarm) with open('../data/alarm.txt','r',encoding='utf-8') as f: for i in f.readlines(): timelength,title,music,repeat1,repeat2,repeat3,repeat4,repeat5,repeat6,repeat7,description,running1=i.split(':/:') alarm=Alarm(alarmScrollAreaWidget,int(timelength),title,int(music),list(map(bool,map(int,[repeat1,repeat2,repeat3,repeat4,repeat5,repeat6,repeat7]))),description,bool(int(running1))) alarm.launch() alarm.show() alarmList.append(alarm)
|