PyQt5 QDateEdit – 获取输入日期
在本文中,我们将了解如何获取 QDateEdit 的输入日期。用户可以借助光标和键盘将日期设置为日期编辑,但有时需要以编程方式设置初始日期或日期以在记录中显示日期,这可以借助 setDate 方法来完成。
为了做到这一点,我们使用带有 QDateEdit 对象的日期方法
Syntax : date.date()
Argument : It takes no argument
Return : It returns QDate object
下面是实现
Python3
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 500, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for components
def UiComponents(self):
# creating a QDateEdit widget
date = QDateEdit(self)
# setting geometry of the date edit
date.setGeometry(100, 100, 150, 40)
# date
d = QDate(2020, 6, 10)
# setting date to the date edit
date.setDate(d)
# creating a label
label = QLabel("GeeksforGeeks", self)
# setting geometry
label.setGeometry(100, 150, 200, 60)
# making label multiline
label.setWordWrap(True)
# getting current date
value = date.date()
# setting text to the label
label.setText("Date : " + str(value))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :