qt获取当前时间(qt时间转化成时间戳)

本文利用PySide6中的LCD数字组件实时显示日期和时间。这里最重要的是实时更新时间显示,主要使用QTimer类。一、界面设计日期/时间编辑组件和lcd数字组

本文利用PySide6中的LCD数字组件实时显示日期和时间。

这里最重要的是实时更新时间显示,主要使用QTimer类。

一、界面设计

日期/时间编辑组件和lcd数字组件(对象名:LCD)

qt获取当前时间(qt时间转化成时间戳)插图

二、程序代码

import sysfrom PySide6 import QtCore,QtGui,QtWidgetsfrom Ui_lcddt import Ui_MainWindowclass MainWindow(QtWidgets.QMainWindow,Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) #设置时间显示格式 self.dateTimeEdit.setDisplayFormat("yyyy-MM-dd hh:mm:ss") #设置可以弹出选择对话框 self.dateTimeEdit.setCalendarPopup(True) #设置当前时间 self.dateTimeEdit.setDateTime(QtCore.QDateTime.currentDateTime()) self.dateTimeEdit.setFont(QtGui.QFont("Times",20)) #设置lcd显示位数,字体大小 self.lcd.setDigitCount(19) self.lcd.setFrameShape(QtWidgets.QFrame.NoFrame) self.lcd.setFont(QtGui.QFont("Times",20)) #每秒更新时间 self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update) self.timer.start(1000) def update(self): self.dateTimeEdit.setDateTime(QtCore.QDateTime.currentDateTime()) self.lcd.display(QtCore.QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) if __name__ == '__main__': app=QtWidgets.QApplication(sys.argv) mywin=MainWindow() mywin.setWindowTitle(" Date Time LCD Demo ") mywin.show() sys.exit(app.exec_())三、运行效果:

qt获取当前时间(qt时间转化成时间戳)插图(1)import sys from pyside 6 import Qt core,QtGui,QtWidgets from Ui _ LC DDT import Ui _ main window class main window(Qt widgets。QMainWindow,Ui _ main window):def _ _ init _ _(self):super()。_ _ init _ _ () self.setupui (self) #设置时间显示格式self . datetime edit . setdisplayformat(& # 34;yyyy-MM-DD hh:MM:ss & # 34;)#设置可弹出选择对话框self . datetime edit . setcalendarpopup(true)#设置当前时间self . datetime edit . setdatetime(Qt core . qdatetime . current datetime())self . datetime edit . Set font(Qt GUI . q font(& # 34;时代& # 34;、20)) #设置lcd显示位数、字体大小self . LCD . setdigitcount(19)self . LCD . setframeshape(Qt widgets . q frame . no frame)self . LCD . Set font(Qt GUI . q font(& # 34;时代& # 34;,20)) #每秒更新时间self . timer = Qt core . q timer(self)self . timer . time out . connect(self . update)self . timer . start(1000)def update(self):self . datetime edit . setdatetime(Qt core。qdatetime . current datetime())self . LCD . display(Qt core。QDateTime.currentDateTime()。toString(& # 34;yyyy-MM-DD hh:MM:ss & # 34;))if _ _ name _ _ = = & # 39_ _ main _ _ & # 39:app=QtWidgets。q application(sys . argv)mywin = main window()mywin . setwindowtitle(& # 34;日期时间LCD演示& # 34;)mywin . show()sys . exit(app . exec _())III。运行效果:

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

作者:美站资讯,如若转载,请注明出处:https://www.meizw.com/n/62640.html

发表回复

登录后才能评论