QCalendarWidget
in PyQt5 is a widget that provides a monthly-based calendar view, allowing the user to select dates and navigate through months and years. It's a versatile widget useful for applications that require date inputs. Here's a basic guide on how to use QCalendarWidget
in a PyQt5 application.
First, you need to import the necessary modules:
import sys from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QVBoxLayout
You can create a class for your main window that includes the QCalendarWidget
:
class MainWindow(QWidget): def __init__(self): super().__init__() # Create Calendar Widget self.calendar = QCalendarWidget(self) # Optional: Set a specific date format, selection mode, etc. self.calendar.setGridVisible(True) # Layout layout = QVBoxLayout() layout.addWidget(self.calendar) self.setLayout(layout) self.setWindowTitle("Calendar Example") self.setGeometry(300, 300, 400, 300)
Finally, initialize and run your application:
if __name__ == '__main__': app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_())
self.calendar.clicked.connect(self.on_date_selected)
can be used to trigger a function when a date is selected.Here is a complete example that includes a function to handle date selection:
import sys from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QVBoxLayout from PyQt5.QtCore import QDate class MainWindow(QWidget): def __init__(self): super().__init__() # Create Calendar Widget self.calendar = QCalendarWidget(self) self.calendar.setGridVisible(True) self.calendar.clicked.connect(self.on_date_selected) # Layout layout = QVBoxLayout() layout.addWidget(self.calendar) self.setLayout(layout) self.setWindowTitle("Calendar Example") self.setGeometry(300, 300, 400, 300) def on_date_selected(self, date): print(f"Selected Date: {date.toString()}") if __name__ == '__main__': app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_())
This script creates a simple PyQt5 window with a calendar widget. When a date is selected, it prints the selected date to the console. You can expand this by adding more functionality based on your application's needs.
rows zurb-foundation text-extraction file-put-contents rhino-mocks events msdeploy struct android-shape aem