Dec-23-2019, 09:43 PM
(Dec-23-2019, 04:07 PM)Denni Wrote: While @Axel_Erfurt nicely provided a way to do it sadly it is fraught with numerous issues. Now while none of these issues would cause the program not to run, they are ones that are either out-dated, poor/dangerous-implementation, or a bit too complex for the situation at hand. I have used his code and adjusted it to remove these elements and simplify its layout -- I added comments to outline why for some of these as some are just pure style changes (like variable naming and such)
USE_PYSIDE2 = False # Always be explicit with what you Import -- aka importing entire libraries in a single reference is simply lazy coding if USE_PYSIDE2: # I include these 2 simply for sharing code as they are the only 2 that change from PySide2.QtCore import Signal, Slot from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout from PyQt5.QtWidgets import QLineEdit, QPushButton, QMessageBox else: from PyQt5.QtCore import pyqtSignal as Signal from PyQt5.QtCore import pyqtSlot as Slot from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout from PyQt5.QtWidgets import QLineEdit, QPushButton, QMessageBox class LoginPanel(QWidget): def __init__(self, parent): # Using Super( ) is wrong -- see MainWindow below QWidget.__init__(self) self.Parent = parent self.lneName = QLineEdit() self.lneName.setPlaceholderText('User name') self.lnePass = QLineEdit() self.lnePass.setPlaceholderText('Password') self.btnLogin = QPushButton('Login') self.btnLogin.clicked.connect(self.HandleLogin) # Best not to obfuscate what you are doing VBox = QVBoxLayout() VBox.addWidget(self.lneName) VBox.addWidget(self.lnePass) VBox.addWidget(self.btnLogin) # Explicitness makes it easier to understand self.setLayout(VBox) # Always use a Slot decorator for Signal receivers as not doing so can cause # issues based on the circumstances and not doing so is also just lazy coding @Slot() def HandleLogin(self): # This allows you to easily validate what is being returned # should any minor issues occur also makes the code easier to read LoginName = self.lneName.text() LoginPass = self.lnePass.text() if self.Validate(LoginName, LoginPass): QMessageBox.information(self, 'Info', 'Valid User Name and Password') # Here you could add a call to swap CenterPanes else: QMessageBox.warning(self, 'Error', 'Bad user or password') def Validate(self, LoginName, LoginPass): # Breaking this out into its own method so that changes can be made to this # such converting it into a database call can be handled without changing # the main flow of code and it also makes things easier to read above RetVal = False if (LoginName == 'foo' and LoginPass == 'bar'): RetVal = True return RetVal class MainWindow(QMainWindow): def __init__(self): # Using Super( ) is wrong as it was designed for a specific rare # situation and it makes your code more complex and adds additional # issues over using the simpler explicit method QMainWindow.__init__(self) self.CenterPane = LoginPanel(self) self.setCentralWidget(self.CenterPane) # Now if you wanted a different CenterPane after the LoginPanel # you can simply make another Class to handle that and replace # the current CenterPane with the new CenterPane if __name__ == '__main__': # If not using Command Line arguments then you do not need sys.argv # and if using Command Line arguments use argparser library instead MainEventThread = QApplication([]) MainApp = MainWindow() MainApp.show() # This is the Qt5 way of handling it now MainEventThread.exec()
Thanks I appreciate it! But Im more looking for how to have a lineedit save the input from the user in a program, for example. I have a Qlineedit widget and the user enters in "creampie" into the line edit and they click a confirm. What would be the best way to have the program save the input in a lineedit (Hence "creampie").