2024年5月15日第四十九篇
今天学习了python的图形化界面的写法和matlab的语法,团队作业又进行了一次优化,基本完成了个人任务。
import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QCalendarWidget, QTextBrowser, QDialog, QLabel, QLineEdit, QMessageBox from PyQt5.QtCore import QDate, Qt, QTimer, QDateTime from cnlunar import Lunar from datetime import datetime import mysql.connector class LoginDialog(QDialog): def __init__(self): super().__init__() self.setWindowTitle("登录") layout = QVBoxLayout() self.usernameInput = QLineEdit() self.usernameInput.setPlaceholderText("请输入用户名") layout.addWidget(self.usernameInput) self.passwordInput = QLineEdit() self.passwordInput.setPlaceholderText("请输入密码") self.passwordInput.setEchoMode(QLineEdit.Password) layout.addWidget(self.passwordInput) self.loginButton = QPushButton("登录") self.loginButton.clicked.connect(self.login) layout.addWidget(self.loginButton) self.setLayout(layout) def login(self): # 连接到数据库 try: conn = mysql.connector.connect( host="localhost", user="root", password="1234", database="loginweb" ) cursor = conn.cursor() # 查询数据库中是否存在匹配的用户名和密码 query = "SELECT * FROM login WHERE username = %s AND password = %s" cursor.execute(query, (self.usernameInput.text(), self.passwordInput.text())) if cursor.fetchone(): QMessageBox.information(self, "登录成功", "登录成功!") self.accept() # 验证通过,关闭对话框 else: QMessageBox.warning(self, "登录失败", "用户名或密码错误!") cursor.close() conn.close() except mysql.connector.Error as err: QMessageBox.critical(self, "数据库错误", f"数据库连接错误: {err}") class LunarCalendarWidget(QCalendarWidget): def paintCell(self, painter, rect, date): # Call the base class paintCell() to ensure proper rendering super().paintCell(painter, rect, date) # Get the Lunar date lunar_date = Lunar(datetime(date.year(), date.month(), date.day())).lunarDayCn # Draw the Lunar date within the calendar cell painter.drawText(rect, 0x0000, lunar_date) class CalendarWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.calendar = LunarCalendarWidget() self.calendar.selectionChanged.connect(self.updateDateInfo) layout.addWidget(self.calendar) self.dateInfo = QTextBrowser() layout.addWidget(self.dateInfo) self.clock = QTextBrowser() # Add a QTextBrowser for the clock layout.addWidget(self.clock) self.showWeekButton = QPushButton("Show Week") self.showWeekButton.clicked.connect(self.showWeek) layout.addWidget(self.showWeekButton) self.resetButton = QPushButton("Reset") self.resetButton.clicked.connect(self.resetView) layout.addWidget(self.resetButton) self.initial_range = None # Initial display range self.saveInitialRange() self.setLayout(layout) # Start a QTimer to update the clock every second self.timer = QTimer(self) self.timer.timeout.connect(self.updateClock) self.timer.start(1000) def updateDateInfo(self): selected_date = self.calendar.selectedDate() current_datetime = datetime(selected_date.year(), selected_date.month(), selected_date.day()) lunar = Lunar(current_datetime) lunar_info = f"农历: {lunar.lunarYearCn} {lunar.year8Char}[{lunar.chineseYearZodiac}]年 {lunar.lunarMonthCn}{lunar.lunarDayCn}日\n" lunar_info += f"今日节日: {lunar.get_legalHolidays()} {lunar.get_otherHolidays()} {lunar.get_otherLunarHolidays()}\n" lunar_info += f"八字: {lunar.year8Char} {lunar.month8Char} {lunar.day8Char} {lunar.twohour8Char}\n" lunar_info += f"今日节气: {lunar.todaySolarTerms}\n" lunar_info += f"季节: {lunar.lunarSeason}\n" lunar_info += f"宜: {lunar.goodThing}\n" lunar_info += f"忌: {lunar.badThing}\n" self.dateInfo.setText(lunar_info) def showWeek(self): selected_date = self.calendar.selectedDate() first_day_of_week = selected_date.addDays(-selected_date.dayOfWeek()) last_day_of_week = first_day_of_week.addDays(6) self.calendar.setDateRange(first_day_of_week, last_day_of_week) def resetView(self): if self.initial_range: self.calendar.setDateRange(*self.initial_range) def saveInitialRange(self): self.initial_range = (self.calendar.minimumDate(), self.calendar.maximumDate()) def updateClock(self): # Get the current date and time current_date_time = QDateTime.currentDateTime().toString(Qt.DefaultLocaleLongDate) # Display the current date and time in the clock QTextBrowser self.clock.setText(current_date_time) class MainWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() self.calendarWidget = CalendarWidget() layout.addWidget(self.calendarWidget) self.loginButton = QPushButton("登录") self.loginButton.clicked.connect(self.showLoginDialog) layout.addWidget(self.loginButton) self.weather = QLabel("Weather: 晴") layout.addWidget(self.weather) self.setLayout(layout) self.setWindowTitle('农历日历') self.setGeometry(100, 100, 800, 600) def showLoginDialog(self): login_dialog = LoginDialog() if login_dialog.exec_() == QDialog.Accepted: self.loginButton.setText("已登录") if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())