Implement the system desktop clock using Python

Source: Internet
Author: User

A system desktop clock written using Python + PyQT. I just learned Python, but it is relatively simple to write, but the basic functions are also acceptable.

Function:

① The form is at the top of the application, but the time is not visible after other applications are opened.

② Double-click full screen with the left button to enable small screen saver. Double-click again to exit full screen.

③ System Tray Icon. For more information, see PyQt4 \ examples \ desktop \ Program Ray in the PyQt4 source code directory.

④ Right-click the program and minimize it

During use, heart. svg needs to be placed in the directory of the same source code level. [the file can be found in PyQt4 \ examples \ desktop \ mongoray \ images under the PyQt4 sample code directory.

Python2.7 + PyQt4.

__metaclass__ = type#!coding= utf-8#http://blog.csdn.net/gatieme/article/details/17659259#gatiemeimport sysfrom 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_( ))    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.