python qt 日期范围demo
开始选日期,结束选日期
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QCalendarWidget, QLabel, QDialog from PyQt5.QtCore import Qt, QDate class DateRangeWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.start_date_label = QLabel("Start Date: ") self.end_date_label = QLabel("End Date: ") self.start_date_button = QPushButton("Select Start Date") self.end_date_button = QPushButton("Select End Date") layout.addWidget(self.start_date_label) layout.addWidget(self.start_date_button) layout.addWidget(self.end_date_label) layout.addWidget(self.end_date_button) self.start_date_button.clicked.connect(lambda: self.showCalendar(self.updateStartDate)) self.end_date_button.clicked.connect(lambda: self.showCalendar(self.updateEndDate)) self.setLayout(layout) self.setWindowTitle("Date Range Selector") def showCalendar(self, update_function): dialog = QDialog() calendar = QCalendarWidget() calendar.setGridVisible(True) calendar.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader) calendar.clicked.connect(lambda: self.updateAndClose(update_function, calendar.selectedDate(), dialog)) dialog.layout = QVBoxLayout() dialog.layout.addWidget(calendar) dialog.setLayout(dialog.layout) dialog.exec_() def updateAndClose(self, update_function, date, dialog): update_function(date) dialog.close() def updateStartDate(self, date): self.start_date_label.setText(f"Start Date: {date.toString(Qt.ISODate)}") def updateEndDate(self, date): self.end_date_label.setText(f"End Date: {date.toString(Qt.ISODate)}") if __name__ == '__main__': app = QApplication(sys.argv) date_range_widget = DateRangeWidget() date_range_widget.show() sys.exit(app.exec_())