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_())
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2023-05-15 5月15日