非常抱歉,这是由于在代码中忘记导入`QLineEdit`模块导致的错误。请添加以下代码行来修复问题:from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QProgressBar, QLineEdit
同时,如果您使用了`QMessageBox`,请确保也导入了对应的模块:from PySide6.QtWidgets import QMessageBox
以下是修正后的代码:
import sys
from PySide6.QtCore import Qt, QTimer
from PySide6.QtGui import QFont, QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QProgressBar, QLineEdit, QMessageBox
class WelcomeWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("欢迎窗体")
self.setGeometry(0, 0, 1400, 900)
self.setCentralWidget(QWidget())
layout = QVBoxLayout()
# 欢迎隶书字样
welcome_label = QLabel("欢迎")
welcome_label.setAlignment(Qt.AlignCenter)
welcome_font = QFont("隶书", 48)
welcome_label.setFont(welcome_font)
layout.addWidget(welcome_label)
# 背景图设置
background_pixmap = QPixmap("background.jpg") # 替换为你自己的图片路径
background_label = QLabel(self)
background_label.setPixmap(background_pixmap.scaled(self.size()))
background_label.lower()
# 进度条设置
self.progress_bar = QProgressBar()
self.progress_bar.setAlignment(Qt.AlignCenter)
self.progress_bar.setTextVisible(True)
layout.addWidget(self.progress_bar)
self.centralWidget().setLayout(layout)
self.timer = QTimer()
self.timer.timeout.connect(self.update_progress)
self.timer.start(1000)
def update_progress(self):
value = self.progress_bar.value() + 1
if value > 100:
self.timer.stop()
login_window = LoginWindow()
login_window.show()
self.hide()
else:
self.progress_bar.setValue(value)
class LoginWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("登录窗体")
self.setGeometry(0, 0, 1400, 900)
self.setCentralWidget(QWidget())
# 设置背景图
background_pixmap = QPixmap("background.jpg") # 替换为你自己的图片路径
background_label = QLabel(self)
background_label.setPixmap(background_pixmap.scaled(self.size()))
background_label.lower()
# 密码输入框和按钮
self.password_input = QLineEdit()
self.password_input.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton("登录")
self.login_button.clicked.connect(self.check_password)
layout = QVBoxLayout()
layout.addWidget(self.password_input)
layout.addWidget(self.login_button)
self.centralWidget().setLayout(layout)
self.attempts = 0
def check_password(self):
password = self.password_input.text()
if password == "123456": # 替换为正确的密码
main_window = MainWindow()
main_window.show()
self.close()
else:
self.attempts += 1
if self.attempts >= 3:
QMessageBox.critical(self, "错误", "密码错误次数过多,程序将退出。")
sys.exit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("主窗体")
self.setGeometry(0, 0, 1400, 900)
# 设置背景图
background_pixmap = QPixmap("background.jpg") # 替换为你自己的图片路径
background_label = QLabel(self)
background_label.setPixmap(background_pixmap.scaled(self.size()))
background_label.lower()
layout = QVBoxLayout()
for i in range(12):
button = QPushButton(f"按钮{i+1}")
layout.addWidget(button)
self.centralWidget().setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
welcome_window = WelcomeWindow()
welcome_window.show()
sys.exit(app.exec())
再次感谢您的反馈,希望这次能够顺利运行。如果还有其他问题,请随时向我提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |