python GUI庫圖形界面開發(fā)之PyQt5日期時(shí)間控件QDateTimeEdit詳細(xì)使用方法與實(shí)例
QDateTimeEdit是一個(gè)允許用戶編輯日期時(shí)間的控件,可以使用鍵盤上的上下鍵頭按鈕來增加或減少日期的時(shí)間值,QDateTimeEdit通過setDisplayFormat()函數(shù)來設(shè)置顯示的日期時(shí)間格式
QDateTimeEdit類中常用方法 方法 描述 setDisplayFormat 設(shè)置日期的時(shí)間格式 yyyy:代表年份,用4為數(shù)表示 MM:代表月份,取值范圍01-12 dd:代表日,取值范圍01-31 HH:代表小時(shí),取值范圍00-23 mm:代表分鐘,取值范圍00-59 ss:代表秒,取值范圍00-59 setMinimumDate() 設(shè)置控件的最小日期 setMaximumDate() 設(shè)置控件的最大日期 time() 返回編輯的時(shí)間 date() 返回編輯的日期 PyQt5日期時(shí)間控件QDateTimeEdit實(shí)例一import sysfrom PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtCore import QDate, QDateTime , QTimeclass DateTimeEditDemo(QWidget): def __init__(self): super(DateTimeEditDemo, self).__init__() self.initUI() def initUI(self): #設(shè)置窗口的標(biāo)題與初始大小 self.setWindowTitle(’QDateTimeEdit例子’) self.resize(300, 90) #垂直布局 vlayout = QVBoxLayout() #實(shí)例化編輯時(shí)間日期的控件 #默認(rèn)下,不指定日期的時(shí)間,系統(tǒng)會(huì)設(shè)置一個(gè)和本地相同的日期時(shí)間格式,時(shí)間默認(rèn)2000年1月1日0時(shí)0分0秒 dateTimeEdit = QDateTimeEdit(self) #指定當(dāng)前日期時(shí)間為控件的日期時(shí)間 dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self) #指定當(dāng)前地日期為控件的日期,注意沒有指定時(shí)間 dateEdit = QDateTimeEdit(QDate.currentDate(), self) #指定當(dāng)前地時(shí)間為控件的時(shí)間,注意沒有指定日期 timeEdit = QDateTimeEdit(QTime.currentTime(), self) # 設(shè)置日期時(shí)間格式,可以選擇/ . : -等符號自定義數(shù)據(jù)連接符 dateTimeEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss') dateTimeEdit2.setDisplayFormat('yyyy/MM/dd HH-mm-ss') dateEdit.setDisplayFormat('yyyy.MM.dd') timeEdit.setDisplayFormat('HH:mm:ss') #布局控件添加,設(shè)置主窗口的布局 vlayout.addWidget( dateTimeEdit ) vlayout.addWidget( dateTimeEdit2) vlayout.addWidget( dateEdit ) vlayout.addWidget( timeEdit ) self.setLayout(vlayout)if __name__ == ’__main__’: app = QApplication(sys.argv) demo = DateTimeEditDemo() demo.show() sys.exit(app.exec_())PyQt5日期時(shí)間控件實(shí)例一代碼解析:
在默認(rèn)情況下,如果QDateTimeEdit類構(gòu)造時(shí)不指定日期時(shí)間,那么系統(tǒng)會(huì)為其設(shè)置一個(gè)和本地相同的日期時(shí)間格式,并且值為2000年1月1日0時(shí)0分0秒,也可以手動(dòng)指定控件顯示的日期時(shí)間
#默認(rèn)下,不指定日期的時(shí)間,系統(tǒng)會(huì)設(shè)置一個(gè)和本地相同的日期時(shí)間格式,時(shí)間默認(rèn)2000年1月1日0時(shí)0分0秒 dateTimeEdit = QDateTimeEdit(self) #指定當(dāng)前日期時(shí)間為控件的日期時(shí)間 dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self) #指定當(dāng)前地日期為控件的日期,注意沒有指定時(shí)間 dateEdit = QDateTimeEdit(QDate.currentDate(), self) #指定當(dāng)前地時(shí)間為控件的時(shí)間,注意沒有指定日期 timeEdit = QDateTimeEdit(QTime.currentTime(), self)
效果如下圖
設(shè)置日期時(shí)間格式,如果不想使用系統(tǒng)默認(rèn)的格式,可以通過setDisplayFormat()來定義日期時(shí)間格式
# 設(shè)置日期時(shí)間格式,可以選擇/ . : -等符號自定義數(shù)據(jù)連接符 dateTimeEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss') dateTimeEdit2.setDisplayFormat('yyyy/MM/dd HH-mm-ss') dateEdit.setDisplayFormat('yyyy.MM.dd') timeEdit.setDisplayFormat('HH:mm:ss')
顯示效果如圖
import sysfrom PyQt5.QtCore import QDate,QDateTime,QTimefrom PyQt5.QtWidgets import *from PyQt5.QtGui import *class DateTimeEditDemo(QWidget): def __init__(self): super(DateTimeEditDemo, self).__init__() self.initUI() def initUI(self): #設(shè)置標(biāo)題與初始大小 self.setWindowTitle(’QDateTimeEdit 例子’) self.resize(300,90) #垂直布局 layout=QVBoxLayout() #創(chuàng)建日期時(shí)間空間,并把當(dāng)前日期時(shí)間賦值,。并修改顯示格式 self.dateEdit=QDateTimeEdit(QDateTime.currentDateTime(),self) self.dateEdit.setDisplayFormat(’yyyy-MM-dd HH:mm:ss’) #設(shè)置日期最大值與最小值,在當(dāng)前日期的基礎(chǔ)上,后一年與前一年 self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365)) self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365)) #設(shè)置日歷控件允許彈出 self.dateEdit.setCalendarPopup(True) #當(dāng)日期改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.dateChanged.connect(self.onDateChanged) #當(dāng)日期時(shí)間改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged) #當(dāng)時(shí)間改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.timeChanged.connect(self.onTimeChanged) #創(chuàng)建按鈕并綁定一個(gè)自定義槽函數(shù) self.btn=QPushButton(’獲得日期和時(shí)間’) self.btn.clicked.connect(self.onButtonClick) #布局控件的加載與設(shè)置 layout.addWidget(self.dateEdit) layout.addWidget(self.btn) self.setLayout(layout) #日期發(fā)生改變時(shí)執(zhí)行 def onDateChanged(self,date): #輸出改變的日期 print(date) #無論是日期還是時(shí)間改變都執(zhí)行 def onDateTimeChanged(self,dateTime): #輸出改變的日期時(shí)間 print(dateTime) #時(shí)間發(fā)生改變執(zhí)行 def onTimeChanged(self,time): #輸出改變的時(shí)間 print(time) def onButtonClick(self): dateTime=self.dateEdit.dateTime() #最大日期 maxDate=self.dateEdit.maximumDate() #最大日期時(shí)間 maxDateTime=self.dateEdit.maximumDateTime() #最大時(shí)間 maxTime=self.dateEdit.maximumTime() #最小日期 minDate = self.dateEdit.minimumDate() #最小日期時(shí)間 minDateTime=self.dateEdit.minimumDateTime() #最小時(shí)間 minTime=self.dateEdit.minimumTime() print(’n選擇時(shí)間日期’) print(’日期時(shí)間=%s’ %str(dateTime)) print(’最大日期=%s’%str(maxDate)) print(’最大日期時(shí)間=%s’%str(maxDateTime)) print(’最大時(shí)間=%s’%str(maxTime)) print(’最小日期=%s’%str(minDate)) print(’最小日期時(shí)間=%s’%str(minDateTime)) print(’最小時(shí)間=%s’%str(minTime))if __name__ == ’__main__’: app=QApplication(sys.argv) demo=DateTimeEditDemo() demo.show() sys.exit(app.exec_())
效果圖如下
QDateEdit和QTimeEdit均繼承自QDateTimeEdit類,他們的許多特性和功能都有QDateTimeEdit類提供,設(shè)置格式是要注意:
QDateEdit用來編輯控件的日期,年月日
QTimeEdit用來編輯控件的時(shí)間,時(shí)分秒
如果要同時(shí)操作日期時(shí)間,請使用QDateTimeEdit
設(shè)置彈出日歷時(shí)要注意:用來彈出日歷的類只有QDateTimeEdit和QDateEdit,而QTimeEdit類雖然在語法上可以設(shè)置彈出日歷,但是不起作用………………
#設(shè)置日歷控件允許彈出self.dateEdit.setCalendarPopup(True)
設(shè)置日期時(shí)間范圍,設(shè)置日期時(shí)間為今天,日歷游戲范圍為:【今天-365,今天+365】
#設(shè)置日期最大值與最小值,在當(dāng)前日期的基礎(chǔ)上,后一年與前一年self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365))self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))獲取日期時(shí)間
可以通過date(),datetime()等方法來獲取日期時(shí)間對象,如果要獲取年月日等信息,則可以調(diào)用QDate的year(),month(),day()等函數(shù)
dateTime=self.dateEdit.dateTime() #最大日期 maxDate=self.dateEdit.maximumDate() #最大日期時(shí)間 maxDateTime=self.dateEdit.maximumDateTime() #最大時(shí)間 maxTime=self.dateEdit.maximumTime() #最小日期 minDate = self.dateEdit.minimumDate() #最小日期時(shí)間 minDateTime=self.dateEdit.minimumDateTime() #最小時(shí)間 minTime=self.dateEdit.minimumTime() print(’n選擇時(shí)間日期’) print(’日期時(shí)間=%s’ %str(dateTime)) print(’最大日期=%s’%str(maxDate)) print(’最大日期時(shí)間=%s’%str(maxDateTime)) print(’最大時(shí)間=%s’%str(maxTime)) print(’最小日期=%’%str(minDate)) print(’最小日期時(shí)間=%s’%str(minDateTime)) print(’最小時(shí)間=%s’%str(minTime))信號與槽函數(shù)
QDateTimeEdit控件常用的信號是dateChanged,dateTimeChanged,TimeChanged,分別在改變?nèi)掌冢掌跁r(shí)間,時(shí)間時(shí)發(fā)射
通過以下代碼設(shè)置控件的信號連接槽函數(shù)
#當(dāng)日期改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.dateChanged.connect(self.onDateChanged) #當(dāng)日期時(shí)間改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged) #當(dāng)時(shí)間改變時(shí)觸發(fā)槽函數(shù) self.dateEdit.timeChanged.connect(self.onTimeChanged)
槽函數(shù)如下
#日期發(fā)生改變時(shí)執(zhí)行 def onDateChanged(self,date): #輸出改變的日期 print(date) #無論是日期還是時(shí)間改變都執(zhí)行 def onDateTimeChanged(self,dateTime): #輸出改變的日期時(shí)間 print(dateTime) #時(shí)間發(fā)生改變執(zhí)行 def onTimeChanged(self,time): #輸出改變的時(shí)間 print(time)
本文詳細(xì)介紹PyQt5日期時(shí)間控件QDateTimeEdit詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5日期時(shí)間控件的使用方法請查看下面的相關(guān)鏈接
相關(guān)文章:
1. 告別AJAX實(shí)現(xiàn)無刷新提交表單2. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享3. 詳解盒子端CSS動(dòng)畫性能提升4. WML語言的基本情況5. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. HTML中的XML數(shù)據(jù)島記錄編輯與添加8. CSS代碼檢查工具stylelint的使用方法詳解9. 淺談CSS不規(guī)則邊框的生成方案10. html中的form不提交(排除)某些input 原創(chuàng)
