PyQt5 QCalendarWidget - Showing today date

PyQt5 QCalendarWidget - Showing today date

To display today's date in a QCalendarWidget using PyQt5, you can use the setSelectedDate() method to select the current date. The QDate class provides a convenient way to work with dates in PyQt5, and you can use QDate.currentDate() to get the current date.

Here's an example of how to display a QCalendarWidget with today's date selected:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QCalendarWidget from PyQt5.QtCore import QDate class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # Create a QVBoxLayout instance layout = QVBoxLayout(self) # Create a QCalendarWidget self.calendar = QCalendarWidget(self) # Set the selected date to today self.calendar.setSelectedDate(QDate.currentDate()) # Add the calendar to the layout layout.addWidget(self.calendar) # Window settings self.setGeometry(300, 300, 350, 300) self.setWindowTitle('QCalendarWidget Example') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() 

In this example:

  • A QCalendarWidget is created.
  • QDate.currentDate() is used to get today's date.
  • The calendar's selected date is set to today's date using setSelectedDate().
  • The calendar is added to a QVBoxLayout and set to the main QWidget.
  • Basic window settings are configured.

When you run this application, it will display a calendar with today's date highlighted as the selected date.


More Tags

android-xml xampp-vm zip4j filestream mql5 macos-high-sierra buffer-overflow elementtree nestedscrollview embedded-resource

More Programming Guides

Other Guides

More Programming Examples