首页 > 代码库 > pyqt 动态显示时间方法例子学习

pyqt 动态显示时间方法例子学习

# -*- coding: utf-8 -*-

# python:2.x

__author__ = ‘Administrator‘

import sys,datetime

from PyQt4.QtCore import Qt

from PyQt4 import QtGui,QtCore,Qt

from ti import Ui_Form

class Example(QtGui.QDialog,Ui_Form):

    def __init__(self,parnet=None):

        super(Example, self).__init__(parnet)

        self.setupUi(self)

        tim=QtCore.QTimer(self)

        tim.timeout.connect(self.times1)

        tim.timeout.connect(self.times2)

        tim.start(1)

 

    def times1(self):#第一种简单

        a=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)

        self.lineEdit.setText(a)

    def times2(self):#第二种

        a=QtCore.QTime.currentTime()

        a1=Qt.QDate.currentDate()

        a2=a1.toString(‘yyyy-MM-dd‘)+‘ ‘+a.toString(‘hh:mm:ss‘)

        self.lineEdit_2.setText(a2)

def main():

    app = QtGui.QApplication(sys.argv)

    ex = Example()

    ex.show()

    sys.exit(app.exec_())

main()

结果如下: