PyQt5 QDateEdit – 日期更改信号
在本文中,我们将了解如何获取 QDateEdit 的日期更改信号。当日期编辑的日期发生更改时,会出现日期更改信号,用户可以借助光标或键盘更改日期,我们也可以借助setDate
方法以编程方式为其设置日期。
为了做到这一点,我们使用带有 QDateEdit 对象的dateChanged
方法
Syntax : date.dateChanged.connect(method)
Argument : It takes method as argument
Return : It returns None
下面是实现
# 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)
# creating a label
label = QLabel("GeeksforGeeks", self)
# setting geometry
label.setGeometry(100, 150, 200, 60)
# making label multiline
label.setWordWrap(True)
# date change signal
date.dateChanged.connect(lambda: method())
# method called when signal emitted
def method():
# setting text to the label
label.setText("Date Changed Signal")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :