python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘
用Python + PyQT寫的一個(gè)系統(tǒng)桌面時(shí)鐘,剛學(xué)習(xí)Python,寫的比較簡陋,但是基本的功能還可以。
功能:
①窗體在應(yīng)用程序最上層,不用但是打開其他應(yīng)用后看不到時(shí)間
②左鍵雙擊全屏,可以做小屏保使用,再次雙擊退出全屏。
③系統(tǒng)托盤圖標(biāo),主要參考PyQt4源碼目錄中的PyQt4\examples\desktop\systray下的程序
④鼠標(biāo)右鍵,將程序最小化
使用時(shí)需要heart.svg放在源代碼同級(jí)目錄下,[文件可在PyQt4示例代碼目錄下PyQt4\examples\desktop\systray\images找到
運(yùn)行需要Python2.7 + PyQt4.
__metaclass__ = type #!coding= utf-8 #http://blog.csdn.net/gatieme/article/details/17659259 #gatieme import sys from PyQt4.QtCore import * from PyQt4.QtGui import * #-------------------------------------------------------------------------------- class SystemTrayIcon(QSystemTrayIcon): """ The systemTrayIcon which uesd to connect the clock """ #---------------------------------------------------------------------------- def __init__(self, mainWindow, parent = None): """ mainWindow : the main window that the system tray icon serves to """ super(SystemTrayIcon, self).__init__(parent) self.window = mainWindow self.setIcon(QIcon("heart.svg")) # set the icon of the systemTrayIcon self.createActions( ) self.createTrayMenu( ) self.connect(self, SIGNAL("doubleClicked"), self.window, SLOT("showNormal")) #self.connect(self, SIGNAL("activated( )"), self, SLOT("slot_iconActivated")) def createActions(self): """ create some action to Max Min Normal show the window """ 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) def createTrayMenu(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) def setVisible(self, visible): self.minimizeAction.setEnabled(not visible) self.maximizeAction.setEnabled(not self.window.isMaximized()) self.restoreAction.setEnabled(self.window.isMaximized() or not visible) super(Window, self).setVisible(visible) def closeEvent(self, event): #if event.button( ) == Qt.RightButton: self.showMessage("Message", "The program will keep running in the system tray. To " "terminate the program, choose <b>Quit</b> in the " "context menu of the system tray entry.", QSystemTrayIcon.Information, 5000) self.window.hide( ) event.ignore( ) def slot_iconActivated(self, reason): if reason == QSystemTrayIcon.DoubleClick: self.wiondow.showNormal( ) #-------------------------------------------------------------------------------- class DigitClock(QLCDNumber): """ the DigitClock show a digit clock int the printer """ #---------------------------------------------------------------------------- def __init__(self, parent = None): """ the constructor function of the DigitClock """ 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( ) # print the time when the clock show self.systemTrayIcon.show( ) # show the SystemTaryIcon when the clock show self.timer = QTimer( ) self.connect(self.timer, SIGNAL("timeout( )"), self.showTime) self.timer.start(1000) self.resize(500, 60) #---------------------------------------------------------------------------- def showTime(self): """ show the current time """ self.date = QDate.currentDate( ) self.time = QTime.currentTime( ) text = self.date.toString("yyyy-MM-dd") + " " + self.time.toString("hh:mm:ss") self.display(text) #---------------------------------------------------------------------------- def mousePressEvent(self, event): """ clicked the left mouse to move the clock clicked the right mouse to hide the clock """ if event.button( ) == Qt.LeftButton: self.dragPosition = event.globalPos( ) - self.frameGeometry( ).topLeft( ) event.accept( ) elif event.button( ) == Qt.RightButton: self.systemTrayIcon.closeEvent(event) #self.systemTrayIcon.hide( ) #self.close( ) def mouseMoveEvent(self, event): """ """ if event.buttons( ) & Qt.LeftButton: self.move(event.globalPos( ) - self.dragPosition) event.accept( ) def keyPressEvent(self, event): """ you can enter "ESC" to normal show the window, when the clock is Maxmize """ if event.key() == Qt.Key_Escape and self.isMaximized( ): self.showNormal( ) def mouseDoubleClickEvent(self, event): """ """ if event.buttons() == Qt.LeftButton: if self.isMaximized( ): self.showNormal( ) else: self.showMaximized( ) if __name__ == "__main__": app = QApplication(sys.argv) digitClock = DigitClock( ) digitClock.show( ) sys.exit(app.exec_( ))
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PyQt5使用QTimer實(shí)現(xiàn)電子時(shí)鐘
- Python+Pyqt實(shí)現(xiàn)簡單GUI電子時(shí)鐘
- Qt繪制簡單時(shí)鐘
- QTimer與QTime實(shí)現(xiàn)電子時(shí)鐘
- Qt編程實(shí)現(xiàn)小時(shí)鐘
- Qt實(shí)現(xiàn)簡易時(shí)鐘
- Qt實(shí)現(xiàn)指針式時(shí)鐘 Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
- Qt設(shè)計(jì)時(shí)鐘效果
- QT實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
- QT5實(shí)現(xiàn)電子時(shí)鐘
相關(guān)文章
使用numpy對(duì)數(shù)組求平均時(shí)如何忽略nan值
這篇文章主要介紹了使用numpy對(duì)數(shù)組求平均時(shí)如何忽略nan值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
這篇文章主要介紹了Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼,需要的朋友可以參考下2016-08-08如何利用python在剪貼板上讀取/寫入數(shù)據(jù)
說起處理數(shù)據(jù)就離不開導(dǎo)入導(dǎo)出,而我們使用Pandas時(shí)候最常用的就是read_excel、read_csv了,下面這篇文章主要給大家介紹了關(guān)于如何利用python在剪貼板上讀取/寫入數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-07-07python pygame實(shí)現(xiàn)五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了python pygame實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Windows安裝Anaconda并且配置國內(nèi)鏡像的詳細(xì)教程
我們?cè)趯W(xué)習(xí) Python 的時(shí)候需要不同的 Python 版本,關(guān)系到電腦環(huán)境變量配置換來換去很是麻煩,所以這個(gè)時(shí)候我們需要一個(gè)虛擬的 Python 環(huán)境變量,這篇文章主要介紹了Windows安裝Anaconda并且配置國內(nèi)鏡像教程,需要的朋友可以參考下2023-01-01使用Python編程分析火爆全網(wǎng)的魷魚游戲豆瓣影評(píng)
本文來為大家介紹如何使用Python爬取影評(píng)的操作,主要是爬取《魷魚游戲》在豆瓣上的一些影評(píng),對(duì)數(shù)據(jù)做一些簡單的分析,用數(shù)據(jù)的角度重新審視下這部劇,有需要的朋友可以借鑒參考下2021-10-10利用Python操作消息隊(duì)列RabbitMQ的方法教程
RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完整的,可復(fù)用的企業(yè)消息系統(tǒng)。他遵循Mozilla Public License開源協(xié)議。下面這篇文章主要給大家介紹了關(guān)于利用Python操作消息隊(duì)列RabbitMQ的方法教程,需要的朋友可以參考下。2017-07-07