PyQt5 QCalendarWidget - Showing it

PyQt5 QCalendarWidget - Showing it

To show a QCalendarWidget in a PyQt5 application, you can simply add it to a main window or any other container and then display the main window.

Here's a step-by-step guide to create a simple PyQt5 application that displays a QCalendarWidget:

  1. Setup:

    First, ensure you have PyQt5 installed:

    pip install pyqt5 
  2. Code Example:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget class App(QMainWindow): def __init__(self): super().__init__() # Create a calendar widget self.calendar = QCalendarWidget(self) # Set the calendar as the central widget of the main window self.setCentralWidget(self.calendar) # Set the dimensions of the main window self.setGeometry(100, 100, 400, 300) # Set the title for the main window self.setWindowTitle('QCalendarWidget Example') if __name__ == '__main__': app = QApplication(sys.argv) window = App() window.show() sys.exit(app.exec_()) 

In this code:

  • We first import the necessary modules.
  • We define a new App class that inherits from QMainWindow.
  • Inside the App class, we initialize a QCalendarWidget and set it as the central widget of the main window.
  • Finally, in the if __name__ == '__main__': block, we create an instance of the App, display the main window using the show() method, and run the application's event loop.

Running the above code will open a PyQt5 window displaying the QCalendarWidget. You can interact with the calendar, select dates, navigate between months, etc.


More Tags

subshell pie-chart moving-average proxyquire ruby-on-rails appbar docker-compose python-turtle gtk json-deserialization

More Programming Guides

Other Guides

More Programming Examples