Python Forum
PyQt5: How do you set the user input of a line edit to a specific variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5: How do you set the user input of a line edit to a specific variable?
#14
(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").
Reply


Messages In This Thread
RE: PyQt5: How do you set the user input of a line edit to a specific variable? - by YoshikageKira - Dec-23-2019, 09:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,504 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,812 May-12-2022, 07:46 PM
Last Post: Extra
  [Tkinter] Help with input from specific devices Noob_hobbyist 3 2,435 Feb-02-2021, 04:11 AM
Last Post: deanhystad
  Convert combobox user input in to date with tkinter Ame 8 6,870 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Create an identification code from user input PeroPuri 1 1,935 Apr-11-2020, 11:56 AM
Last Post: Larz60+
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,849 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,184 Oct-23-2019, 06:23 PM
Last Post: Denni
  [Tkinter] Registering user's drop down menu choice and finding corresponding line in files ShadeLily 2 2,056 Oct-03-2019, 06:28 PM
Last Post: joe_momma
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,873 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [Tkinter] help please, checking the user input and outputting the result tomkovladko 3 2,784 Mar-05-2019, 08:58 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020