3

Today Button like this

Image of my pop-up calendar widget:

Image of my pop-up calendar widget

I am trying to create simple Gui using PyQt5 in Python with date picker option. I need to add today button in QDateEdit in pop-up QCalendarWidget.

RamG
  • 33
  • 1
  • 4
  • 1
    Can you explain better where that button should be and what should be done when that button is pressed? – eyllanesc Nov 20 '19 at 16:51
  • I want to place the button inside the pop-up calendar. When i press the button the calendar should scroll to the current date. – RamG Nov 20 '19 at 17:06
  • 1
    In what part of the popup? Could you place an image of what you want to get? – eyllanesc Nov 20 '19 at 17:11
  • Thank you eyllanesc. I have added the sample image in my post kindly check it. – RamG Nov 20 '19 at 17:24

2 Answers2

4

You must add the button to the QCalendarWidget through the layout, and when the button is pressed set the QDate::currentDate() as selectedDate of the QCalendarWidget:

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

You can decide to create a custom button to handle the popup even.

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()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Gabriel J
  • 66
  • 7
  • Your solution is fine to me, but I'd suggest you to avoid fixed positions and prefer layouts, as widgets can have different size hints and margins in other people's systems, resulting in serious [displaying issues](https://i.stack.imgur.com/MMLsU.png). – musicamante Nov 20 '19 at 23:56
  • Thanks bro. really appreciate for that observation! – Gabriel J Nov 21 '19 at 00:37