PyQt5 QDateEdit – 时变信号
在本文中,我们将了解如何获得 QDateEdit 的时间变化信号。与普通的日期不同,我们可以在setDateTime
方法的帮助下为其设置日期时间。但是我们也可以只设置时间而不用通过setTime
方法更改日期。每当更改日期编辑的时间时,就会发出此信号。
为了做到这一点,我们使用带有 QDateEdit 对象的timeChanged
方法
Syntax : date.timeChanged.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)
# time change signal
date.timeChanged.connect(lambda: method())
# method called when signal emitted
def method():
# setting text to the label
label.setText("Time Changed Signal")
# date time
time = QTime(11, 20, 0)
# setting time
date.setTime(time)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :