基于PyQt实现用户登录弹窗及登录历史存储需求问询
Hey there! Let’s build out that PyQt4 login dialog with login history tracking you need. I’ve completed your partial code and added all the functionality to store user login records—check it out:
import sys from PyQt4.QtGui import QDialog, QApplication, QComboBox, QPushButton, QVBoxLayout, QLabel from PyQt4.QtCore import Qt class Login(QDialog): def __init__(self, parent=None): super(Login, self).__init__(parent) # Predefined user list and login history storage self.contributors = ['', 'A', 'B', 'C'] self.login_history = [] # Use a list to track logins; swap to set() for auto-duplicate removal # Window setup self.setWindowTitle("Login") # Create UI components self.user_label = QLabel("Select Your Name:") self.user_combo = QComboBox() self.user_combo.addItems(self.contributors) self.login_btn = QPushButton("Login") self.login_btn.clicked.connect(self.handle_login) # Arrange components with layout layout = QVBoxLayout() layout.addWidget(self.user_label) layout.addWidget(self.user_combo) layout.addWidget(self.login_btn) self.setLayout(layout) def handle_login(self): selected_user = self.user_combo.currentText() if selected_user: # Ensure a valid name is chosen # Add to login history (skip duplicate check if you want to track every attempt) if selected_user not in self.login_history: self.login_history.append(selected_user) print(f"Welcome {selected_user}! Login successful.") print(f"Current login history: {self.login_history}") self.accept() # Close dialog and signal success to main app else: print("Please pick a valid name first!") if __name__ == "__main__": app = QApplication(sys.argv) login_dialog = Login() if login_dialog.exec_() == QDialog.Accepted: print("Loading main application...") sys.exit(app.exec_())
Quick Breakdown of Key Bits
- Login History: I used a list
self.login_historyto store successful logins. If you want automatic duplicate removal (since sets don’t allow duplicates), just swap[]toset()and use.add(selected_user)instead of.append(). - UI Flow: The dropdown (
QComboBox) lets users pick from your predefined names, and the login button triggers the login logic. - Validation: We check that a non-empty name is selected before proceeding—no blank logins allowed!
- Accessing History: After the dialog closes, you can access the login history from the main app using
login_dialog.login_history(like how we check if the login was accepted in the__main__block).
内容的提问来源于stack exchange,提问作者UserA
相关产品推荐
相关产品推荐

