欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python+PyQT实现系统桌面时钟

程序员文章站 2024-02-07 09:11:46
用python + pyqt写的一个系统桌面时钟,刚学习python,写的比较简陋,但是基本的功能还可以。 功能: ①窗体在应用程序最上层,不用但是打开其他应用后看不到...

用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 
 
 
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_( )) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。