【pyqt5】QdateTimeEdit(日期时间)
2024-10-06 04:51:10
返回当前日期和时间设置
from PyQt5 import QtCore, QtWidgets class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setWindowModality(QtCore.Qt.WindowModal)
Dialog.resize(690, 437)
Dialog.setAutoFillBackground(True)
Dialog.setSizeGripEnabled(True)
self.calendarWidget = QtWidgets.QCalendarWidget(Dialog)
self.calendarWidget.setGeometry(QtCore.QRect(230, 30, 431, 291))
self.calendarWidget.setObjectName("calendarWidget")
self.dateEdit = QtWidgets.QDateEdit(Dialog)
self.dateEdit.setGeometry(QtCore.QRect(30, 80, 171, 22))
self.dateEdit.setObjectName("dateEdit")
self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog)
self.dateTimeEdit.setEnabled(True)
self.dateTimeEdit.setGeometry(QtCore.QRect(10, 190, 211, 31))
self.dateTimeEdit.setToolTip("")
self.dateTimeEdit.setWrapping(False)
self.dateTimeEdit.setFrame(True) #边框可见
self.dateTimeEdit.setReadOnly(False) #设置只读模式
self.dateTimeEdit.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows)
self.dateTimeEdit.setSpecialValueText("")
self.dateTimeEdit.setKeyboardTracking(True)
self.dateTimeEdit.setCurrentSection(QtWidgets.QDateTimeEdit.DaySection)
self.dateTimeEdit.setCalendarPopup(True)
self.dateTimeEdit.setCurrentSectionIndex(3)
self.dateTimeEdit.setTimeSpec(QtCore.Qt.LocalTime)
self.dateTimeEdit.setObjectName("dateTimeEdit") self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
UI文件
main文件中设置(将获取当前日期时间的代码放在main文件中,避免QT设计师更改界面时 代码改动)
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets
from Ui_QDataTimeEdit import Ui_Dialog class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.dateTimeEdit.setDateTime(QDateTime.currentDateTime())
self.dateEdit.setDate(QDate.currentDate()) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())
一些常用的技巧:
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.widget)
self.dateTimeEdit.setFrame(False) #去除边框
self.dateTimeEdit.setDate(QtCore.QDate(2018, 10, 25)) #设置日期
self.dateTimeEdit.setTime(QtCore.QTime(11, 0, 0)) #设置时间11:00
self.dateTimeEdit.setObjectName("dateTimeEdit") #设置对象名称 """
方式1:
from PyQt5.QtCore import QDate,QTime
##设置为当前日期
self.dateTimeEdit.setDateTime(QDate.currentDate())
##设置为当前时间
self.dateTimeEdit.setDateTime(QTime.currentTime()) 方式2:
from PyQt5.QtCore import QDateTime
##设置当前日期和时间
self.dateTimeEdit.setDateTime(QDateTime.currentDateTime())
"""
##同理设置dataEdit是一样的
from PyQt5.QtCore import QDate
self.dateEdit.setDate(QDate.currentDate())
打印出不同格式的日期和时间
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt """返回当前日期"""
now = QDate.currentDate()
print(now) #PyQt5.QtCore.QDate(2018, 12, 3)
print(now.toString()) #周一 12月 3 2018
print(now.toString(Qt.ISODate)) #2018-12-03
print(now.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 """返回当前日期和时间"""
datetime = QDateTime.currentDateTime()
print(datetime) #PyQt5.QtCore.QDateTime(2018, 12, 3, 15, 9, 41, 976)
print(datetime.toString()) #周一 12月 3 15:09:41 2018
print(datetime.toString(Qt.ISODate)) #2018-12-03T15:09:41
print(datetime.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 15:09:41 """返回当前时间"""
time = QTime.currentTime()
print(time) #PyQt5.QtCore.QTime(15, 12, 9, 980)
print(time.toString()) #15:12:09
print(time.toString(Qt.ISODate)) #15:12:09
print(time.toString(Qt.DefaultLocaleLongDate)) #15:12:09
最新文章
- sizzle分析记录:属性选择器
- 安装 SciPy 和 scikit-learn 升级pip 及pip基本命令表
- 如何从oc中去获取一个私有的变量.....
- 常用的phpstorm设置
- CentOS 6.0 设置IP地址、网关、DNS
- js清空前后空格
- 机房收费系统之vb报表的模板的制作(一)
- java中线程机制
- zepto.js的基本介绍与使用
- vue-router+elelment-ui,实现导航栏激活高亮
- jQuery-3.事件篇---自定义事件
- 什么是车辆识别代码(VIN)
- Dell Venue 8 Pro启动盘UEFI模式32位启动,备份系统
- mybatis BindingException: Invalid bound statement (not found)
- WEBBASE篇: 第五篇, CSS知识3
- jquery.form.js实现将form提交转为ajax方式提交的方法
- 一个.net程序客户端更新方案
- ArcGIS Desktop水文计算
- Android 搭建ssh服务
- AIX解压ZIP文件