如何在QDateEdit弹出QCalendarWidget弹出窗口中添加“今天”按钮

时间:2019-11-20 16:45:38

标签: python pyqt pyqt5 qcalendarwidget

“今天的按钮”

我的弹出式日历小部件的图像:

Image of my pop-up calendar widget

我试图在Python中使用带有日期选择器选项的PyQt5创建简单的Gui。我需要在弹出的QCalendarWidget的QDateEdit中添加“今天”按钮。

2 个答案:

答案 0 :(得分:2)

必须通过布局将按钮添加到QCalendarWidget,并在按下按钮时将QDate :: currentDate()设置为QCalendarWidget的selectedDate:

import sys

from PyQt5 import QtCore, QtWidgets


class DateEdit(QtWidgets.QDateEdit):
    def __init__(self, parent=None):
        super().__init__(parent, calendarPopup=True)
        self._today_button = QtWidgets.QPushButton(self.tr("Today"))
        self._today_button.clicked.connect(self._update_today)
        self.calendarWidget().layout().addWidget(self._today_button)

    @QtCore.pyqtSlot()
    def _update_today(self):
        self._today_button.clearFocus()
        today = QtCore.QDate.currentDate()
        self.calendarWidget().setSelectedDate(today)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = DateEdit()
    w.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

您可以决定创建一个自定义按钮来处理弹出窗口。

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class EditDate:

    def __init__(self):
        super(EditDate, self).__init__()
        self.app_runner()

    def date_gui(self):
        self.app = QApplication(sys.argv)
        self.win = QDialog()
        self.win.setFixedSize(280, 40)


        self.dateEdit = QDateEdit(self.win)
        self.dateEdit.setFixedSize(230, 40)
        self.dateEdit.move(5, 0)

        self.hideCalendar = QPushButton(self.win)
        self.hideCalendar.setText("Hide")
        self.hideCalendar.move(231, 0)
        self.hideCalendar.setFixedSize(45, 40)

        self.displayCalendar = QPushButton(self.win)
        self.displayCalendar.setText("Date..")
        self.displayCalendar.move(231, 0)
        self.displayCalendar.setFixedSize(45, 40)

        self.calender = QCalendarWidget(self.win)
        self.calender.move(5, 40)

        self.todayButton = QPushButton(self.win)
        self.todayButton.setText("Today")
        self.todayButton.setFixedSize(100, 30)
        self.todayButton.move(5, 200)

        self.calender.hide()
        self.todayButton.hide()

    def button_handling(self):
        self.todayButton.clicked.connect(self.todays_date)
        self.displayCalendar.clicked.connect(self.display_cal)
        self.hideCalendar.clicked.connect(self.hide_cal)

    def hide_cal(self):
        self.calender.hide()
        self.displayCalendar.show()
        self.todayButton.hide()
        self.hideCalendar.hide()
        self.win.setFixedSize(280, 40)

    def display_cal(self):
        self.win.setFixedSize(280, 300)
        self.calender.show()
        self.todayButton.show()
        self.hideCalendar.show()
        self.displayCalendar.hide()

    def todays_date(self):
        self.date = QDate.currentDate()
        self.dateEdit.setDate(self.date)
        self.calender.setSelectedDate(self.date)

    def app_runner(self):
        self.date_gui()
        self.win.show()
        self.button_handling()
        sys.exit(self.app.exec_())

main = EditDate()