import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
def setup_ui(self):
self.setWindowTitle("Qt桌面应用程序")
self.resize(300, 300)
self.label = QLabel(self)
self.label.resize(self.width(), 30)
self.label.setStyleSheet("background-color:cyan")
self.push_button = QPushButton(self)
self.push_button.setText("按钮")
self.push_button.move(20, 40)
self.calendar = QCalendarWidget(self)
self.calendar.move(20, 80)
self.calendar.setDateRange(QDate(2000, 1, 1), QDate(2050, 12, 31))
self.calendar.setDateEditEnabled(True)
self.calendar.setNavigationBarVisible(False)
# self.calendar.setSelectionMode(QCalendarWidget.NoSelection)
self.calendar.setFirstDayOfWeek(Qt.Sunday)
text_char_format = QTextCharFormat()
text_char_format.setFontWeight(1000)
self.calendar.setHeaderTextFormat(text_char_format)
def push_button_clicked():
self.label.setText(
f"展现年月:{self.calendar.yearShown()}/{self.calendar.monthShown()} 选择的日期:{self.calendar.selectedDate().toPyDate()}")
self.push_button.clicked.connect(push_button_clicked)
# self.calendar.activated.connect(lambda x:self.label.setText(f"{x.toPyDate()}"))
# self.calendar.selectionChanged.connect(lambda :self.label.setText(f"{self.calendar.selectedDate().toPyDate()}"))
self.calendar.clicked.connect(lambda x: self.label.setText(f"{x.toPyDate()}"))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())