python+PyQT实现系统桌面时钟
用Python+PyQT写的一个系统桌面时钟,刚学习Python,写的比较简陋,但是基本的功能还可以。
功能:
①窗体在应用程序最上层,不用但是打开其他应用后看不到时间
②左键双击全屏,可以做小屏保使用,再次双击退出全屏。
③系统托盘图标,主要参考PyQt4源码目录中的PyQt4\examples\desktop\systray下的程序
④鼠标右键,将程序最小化
使用时需要heart.svg放在源代码同级目录下,[文件可在PyQt4示例代码目录下PyQt4\examples\desktop\systray\images找到
运行需要Python2.7+PyQt4.
__metaclass__=type #!coding=utf-8 #http://blog.csdn.net/gatieme/article/details/17659259 #gatieme importsys fromPyQt4.QtCoreimport* fromPyQt4.QtGuiimport* #-------------------------------------------------------------------------------- classSystemTrayIcon(QSystemTrayIcon): """ ThesystemTrayIconwhichuesdtoconnecttheclock """ #---------------------------------------------------------------------------- def__init__(self,mainWindow,parent=None): """ mainWindow:themainwindowthatthesystemtrayiconservesto """ super(SystemTrayIcon,self).__init__(parent) self.window=mainWindow self.setIcon(QIcon("heart.svg"))#settheiconofthesystemTrayIcon self.createActions() self.createTrayMenu() self.connect(self,SIGNAL("doubleClicked"),self.window,SLOT("showNormal")) #self.connect(self,SIGNAL("activated()"),self,SLOT("slot_iconActivated")) defcreateActions(self): """ createsomeactiontoMaxMinNormalshowthewindow """ self.minimizeAction=QAction("Mi&nimize",self.window,triggered=self.window.hide) self.maximizeAction=QAction("Ma&ximize",self.window,triggered=self.window.showMaximized) self.restoreAction=QAction("&Restore",self.window,triggered=self.window.showNormal) self.quitAction=QAction("&Quit",self.window,triggered=qApp.quit) defcreateTrayMenu(self): self.trayIconMenu=QMenu(self.window) self.trayIconMenu.addAction(self.minimizeAction) self.trayIconMenu.addAction(self.maximizeAction) self.trayIconMenu.addAction(self.restoreAction) self.trayIconMenu.addSeparator() self.trayIconMenu.addAction(self.quitAction) self.setContextMenu(self.trayIconMenu) defsetVisible(self,visible): self.minimizeAction.setEnabled(notvisible) self.maximizeAction.setEnabled(notself.window.isMaximized()) self.restoreAction.setEnabled(self.window.isMaximized()ornotvisible) super(Window,self).setVisible(visible) defcloseEvent(self,event): #ifevent.button()==Qt.RightButton: self.showMessage("Message", "Theprogramwillkeeprunninginthesystemtray.To" "terminatetheprogram,chooseQuitinthe" "contextmenuofthesystemtrayentry.", QSystemTrayIcon.Information,5000) self.window.hide() event.ignore() defslot_iconActivated(self,reason): ifreason==QSystemTrayIcon.DoubleClick: self.wiondow.showNormal() #-------------------------------------------------------------------------------- classDigitClock(QLCDNumber): """ theDigitClockshowadigitclockinttheprinter """ #---------------------------------------------------------------------------- def__init__(self,parent=None): """ theconstructorfunctionoftheDigitClock """ super(DigitClock,self).__init__(parent) pale=self.palette() pale.setColor(QPalette.Window,QColor(100,180,100)) self.setPalette(pale) self.setNumDigits(19) self.systemTrayIcon=SystemTrayIcon(mainWindow=self) self.dragPosition=None; self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.Popup|Qt.Tool) self.setWindowOpacity(1) self.showTime()#printthetimewhentheclockshow self.systemTrayIcon.show()#showtheSystemTaryIconwhentheclockshow self.timer=QTimer() self.connect(self.timer,SIGNAL("timeout()"),self.showTime) self.timer.start(1000) self.resize(500,60) #---------------------------------------------------------------------------- defshowTime(self): """ showthecurrenttime """ self.date=QDate.currentDate() self.time=QTime.currentTime() text=self.date.toString("yyyy-MM-dd")+""+self.time.toString("hh:mm:ss") self.display(text) #---------------------------------------------------------------------------- defmousePressEvent(self,event): """ clickedtheleftmousetomovetheclock clickedtherightmousetohidetheclock """ ifevent.button()==Qt.LeftButton: self.dragPosition=event.globalPos()-self.frameGeometry().topLeft() event.accept() elifevent.button()==Qt.RightButton: self.systemTrayIcon.closeEvent(event) #self.systemTrayIcon.hide() #self.close() defmouseMoveEvent(self,event): """ """ ifevent.buttons()&Qt.LeftButton: self.move(event.globalPos()-self.dragPosition) event.accept() defkeyPressEvent(self,event): """ youcanenter"ESC"tonormalshowthewindow,whentheclockisMaxmize """ ifevent.key()==Qt.Key_Escapeandself.isMaximized(): self.showNormal() defmouseDoubleClickEvent(self,event): """ """ ifevent.buttons()==Qt.LeftButton: ifself.isMaximized(): self.showNormal() else: self.showMaximized() if__name__=="__main__": app=QApplication(sys.argv) digitClock=DigitClock() digitClock.show() sys.exit(app.exec_())
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。