|
楼主 |
发表于 2024-4-7 22:53:40
|
显示全部楼层
请问下面这段代码有什么问题?
import sys
import random
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QMessageBox, QVBoxLayout, \
QHBoxLayout, QWidget, QProgressBar
from PySide6.QtCore import Qt
class BaseWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Base Window")
self.resize(400, 300)
self.welcome_label = QLabel("Welcome to Base Window!", self)
self.welcome_label.setAlignment(Qt.AlignCenter)
self.username_label = QLabel("Username:", self)
self.username_input = QLineEdit(self)
self.password_label = QLabel("Password:", self)
self.password_input = QLineEdit(self)
self.password_input.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton("Login", self)
self.progress_bar = QProgressBar(self)
self.progress_bar.hide()
layout = QVBoxLayout()
layout.addWidget(self.welcome_label)
layout.addWidget(self.username_label)
layout.addWidget(self.username_input)
layout.addWidget(self.password_label)
layout.addWidget(self.password_input)
layout.addWidget(self.login_button)
layout.addWidget(self.progress_bar)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.login_button.clicked.connect(self.login)
self.error_count = 0
def login(self):
username = self.username_input.text()
password = self.password_input.text()
if username == "1" and password == "1":
self.start_progress_bar()
else:
self.error_count += 1
if self.error_count >= 3:
QMessageBox.critical(self, "Error", "Too many login attempts!")
self.close()
else:
QMessageBox.warning(self, "Error", "Incorrect username or password!")
def start_progress_bar(self):
self.progress_bar.setValue(0)
self.progress_bar.show()
self.timer = self.startTimer(100)
def timerEvent(self, event):
value = self.progress_bar.value() + 1
if value > 100:
self.stop_progress_bar()
self.open_window_a()
else:
self.progress_bar.setValue(value)
def stop_progress_bar(self):
self.killTimer(self.timer)
def open_window_a(self):
self.window_a = WindowA()
self.window_a.show()
self.hide()
class WindowA([BaseWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Window A")
self.resize(600, 400)
self.input_label = QLabel("Enter company name or credit code:", self)
self.input_box = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.result_label = QLabel("Results:", self)
self.result_text = QLabel(self)
self.database_label = QLabel("Enter database name:", self)
self.database_input = QLineEdit(self)
self.compare_button = QPushButton("Compare", self)
layout = QVBoxLayout()
layout.addWidget(self.input_label)
layout.addWidget(self.input_box)
layout.addWidget(self.search_button)
layout.addWidget(self.result_label)
layout.addWidget(self.result_text)
layout.addWidget(self.database_label)
layout.addWidget(self.database_input)
layout.addWidget(self.compare_button)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.search_button.clicked.connect(self.search_company)
self.compare_button.clicked.connect(self.compare_data)
def search_company(self):
input_text = self.input_box.text()
# Simulating search and retrieving results
results = f"Search results for '{input_text}':\nResult 1\nResult 2\nResult 3"
self.result_text.setText(results)
def compare_data(self):
database_name = self.database_input.text()
# Simulating data comparison
diff = random.choice(["Difference 1", "Difference 2", "Difference 3"])
if diff:
self.open_window_b(diff)
else:
QMessageBox.information(self, "Info", "No difference found!")
def open_window_b(self, diff):
self.window_b = WindowB(diff)
self.window_b.show()
self.hide()
class WindowB([BaseWindow):
def __init__(self, diff):
super().__init__()
self.setWindowTitle("Window B")
self.resize(400, 300)
self.diff_label = QLabel(f"Differences found:\n{diff}", self)
self.diff_label.setAlignment(Qt.AlignCenter)
layout = QVBoxLayout()
layout.addWidget(self.diff_label)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = BaseWindow()
window.show()
sys.exit(app.exec())
请问为什么[BaseWindow进度条到100时,不打开WindowA? |
|