PyQt5 QDateTimeEdit – QDateTime 改变信号
在本文中,我们将了解如何获取 QDateTimeEdit 小部件的 QDateTime 更改信号。 QDateTime 基本上是 QDate 和 QTime 的组合,即它同时具有日期和时间。 QDateTimeEdit 小部件用于显示或接收 QDateTime。我们可以在setDateTime
方法的帮助下为它设置 QDateTime。
当 QDateTime 发生变化时发出此信号
为此,我们将使用带有 QDateTimeEdit 对象的dateTimeChanged
方法。
Syntax : datetimeedit.dateTimeChanged.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 QDateTimeEdit widget
datetimeedit = QDateTimeEdit(self)
# setting geometry
datetimeedit.setGeometry(100, 100, 150, 35)
# creating a label
label = QLabel("GeeksforGeeks", self)
# setting geometry to the label
label.setGeometry(100, 160, 200, 60)
# making label multi line
label.setWordWrap(True)
# datetime changed signal when emitted connect to the
# dt_method every time
datetimeedit.dateTimeChanged.connect(lambda: dt_method())
# method called by the datetime
def dt_method():
# getting current datetime
value = datetimeedit.dateTime()
# setting text to the label
label.setText("New DateTime : " + str(value))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :