|
发表于 2024-2-16 23:19:41
|
显示全部楼层
非常抱歉,给您带来了困扰。原因是我在代码中错误地引用了不存在的`QGraphicsDropShadowEffect`类。实际上,在PySide6库中,要添加阴影效果,应该使用`QGraphicsDropShadowEffect`类的子类`QGraphicsEffect`。以下是修复后的代码:
- import sys
- from PySide6.QtCore import Qt, QTimer
- from PySide6.QtGui import QFont, QColor, QPalette, QPixmap, QPainter, QPen, QBrush, \
- QLinearGradient, QPainterPath, QGraphicsEffect, QGraphicsDropShadowEffect
- from PySide6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QProgressBar, QLineEdit, QPushButton, \
- QMessageBox
- from PySide6.QtMultimedia import QMediaContent, QMediaPlayer
- from PySide6.QtCore import QUrl
- class ShadowEffect(QGraphicsEffect):
- def __init__(self, parent=None):
- super().__init__(parent)
- def draw(self, painter):
- shadow = QGraphicsDropShadowEffect()
- shadow.setBlurRadius(20)
- shadow.setOffset(2, 2)
- self.sourcePixmap().fill(Qt.transparent)
- self.drawSource(painter)
- self.sourcePixmap().fill(Qt.transparent)
- painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
- painter.fillRect(self.sourcePixmap().rect(), Qt.transparent)
- painter.setCompositionMode(QPainter.CompositionMode_Source)
- painter.setBrush(QColor(0, 0, 0))
- painter.setPen(Qt.NoPen)
- path = QPainterPath()
- path.addRect(self.sourcePixmap().rect())
- path -= QPainterPath(self.cornerPath(self.sourcePixmap().rect()))
- painter.drawPath(path)
- self.drawSource(painter)
- def cornerPath(self, rect):
- r = 10.0
- path = QPainterPath()
- path.addRoundedRect(rect.x(), rect.y(), rect.width(), rect.height(), r, r)
- return path
- class WelcomeWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.setWindowTitle('欢迎界面')
- self.setGeometry(100, 100, 1400, 900)
- # 设置字体
- font = QFont('隶书', 40, QFont.Bold)
- # 创建标签显示欢迎词
- self.welcome_label = QLabel(self)
- self.welcome_label.setText('欢迎')
- self.welcome_label.setFont(font)
- self.welcome_label.move(700, 400)
- self.welcome_label.setFixedSize(200, 50)
- # 设置阴影效果
- shadow_effect = ShadowEffect()
- self.welcome_label.setGraphicsEffect(shadow_effect)
- # 设置背景图片
- background_label = QLabel(self)
- background_image = QPixmap('背景图片地址')
- background_label.setPixmap(background_image.scaled(self.size()))
- background_label.resize(self.size())
- # 播放背景音乐(使用QMediaPlayer)
- self.player = QMediaPlayer()
- self.player.setMedia(QMediaContent(QUrl.fromLocalFile('背景音乐地址')))
- self.player.play()
- # 创建进度条
- self.progress_bar = QProgressBar(self)
- self.progress_bar.setGeometry(450, 830, 500, 30)
- self.progress_bar.setTextVisible(True)
- self.progress_bar.setStyleSheet("""
- QProgressBar::chunk {
- background-color: #2196F3;
- width: 10px;
- }
- """)
- # 设置定时器每秒更新进度条
- self.timer = QTimer(self)
- self.timer.timeout.connect(self.update_progress)
- self.timer.start(1000)
- def update_progress(self):
- value = self.progress_bar.value()
- if value < 100:
- value += 1
- self.progress_bar.setValue(value)
- else:
- self.timer.stop()
- self.show_login_window()
- def show_login_window(self):
- login_window = QWidget()
- login_window.setWindowTitle('用户登录')
- # 创建用户名和密码输入框
- username_label = QLabel('用户名:', login_window)
- username_label.setFont(QFont('隶书', 20))
- username_entry = QLineEdit(login_window)
- password_label = QLabel('密码:', login_window)
- password_label.setFont(QFont('隶书', 20))
- password_entry = QLineEdit(login_window)
- password_entry.setEchoMode(QLineEdit.Password)
- # 登录按钮功能
- def login():
- username = username_entry.text()
- password = password_entry.text()
- if username == 'admin' and password == '123456':
- QMessageBox.information(login_window, '提示', '登录成功!')
- login_window.close()
- # 进入具体的主程序逻辑写在这里
- else:
- QMessageBox.critical(login_window, '错误', '用户名或密码错误!')
- password_entry.clear()
- login_button = QPushButton('登录', login_window)
- login_button.setFont(QFont('隶书', 20))
- login_button.clicked.connect(login)
- # 使用垂直布局
- layout = QVBoxLayout()
- layout.addWidget(username_label)
- layout.addWidget(username_entry)
- layout.addWidget(password_label)
- layout.addWidget(password_entry)
- layout.addWidget(login_button)
- login_window.setLayout(layout)
- login_window.setGeometry(500, 300, 400, 300)
- login_window.show()
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- welcome_window = WelcomeWindow()
- welcome_window.show()
- sys.exit(app.exec_())
复制代码
请再次确保将代码中的`背景图片地址`和`背景音乐地址`替换为实际的文件路径。如果还有任何疑问,请随时告知。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|