PyQt5 QDateEdit – 获取用户可以输入的最小日期
在本文中,我们将了解如何设置 QDateEdit 的最小日期。有时需要设置最小日期,以便用户无法输入超出最小日期的日期,例如当要求用户输入酒店预订日期时,不应允许用户输入过去的日期,然后需要设置最小值日期出现。可以借助setMinimumDate
方法设置最小日期。
为了做到这一点,我们使用minimumDate
方法和 QDateEdit 对象
Syntax : date.minimumDate()
Argument : It takes no argument
Return : It returns QDate object
下面是实现
# 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, 200, 40)
# alignment
a_flag = Qt.AlignCenter
# setting alignment of date
date.setAlignment(a_flag)
# QDate object
d = QDate(2020, 1, 1)
# setting minimum date
date.setMinimumDate(d)
# text
text = "Geek date edit"
# setting name
date.setAccessibleName(text)
# creating a label
label = QLabel("GeeksforGeeks", self)
# setting geometry
label.setGeometry(100, 150, 200, 60)
# making label multiline
label.setWordWrap(True)
# getting minimum date
value = date.minimumDate()
# setting text to the label
label.setText("Min 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())
输出 :