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?
#1
Hello!

I made a program using PyQt5. The main problem however is trying to get the program to read a user input in the Qlineedit widget

my goal is to transfer the user input of the Qlinedit to a variable or a database (something.db file) via confirmation button like in a registration form or such.

I don't exactly know how to get to make a program that can read the user input from QlineEdit and transfer that said input to a variable or a database. Please help! A snippet of my code below:

    self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
    self.lineEdit.setGeometry(QtCore.QRect(260, 310, 241, 31))
    self.lineEdit.setObjectName("lineEdit")
    self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
    self.lineEdit_2.setGeometry(QtCore.QRect(260, 340, 241, 31))
    self.lineEdit_2.setObjectName("lineEdit_2")
    self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
thanks in advance!!
Reply
#2
An Example for a login

from PyQt5 import QtWidgets

class Login(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Login, self).__init__(parent)
        self.textName = QtWidgets.QLineEdit(self)
        self.textName.setPlaceholderText("User name")
        self.textPass = QtWidgets.QLineEdit(self)
        self.textPass.setPlaceholderText("Password")
        self.buttonLogin = QtWidgets.QPushButton('Login', self)
        self.buttonLogin.clicked.connect(self.handleLogin)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.textName)
        layout.addWidget(self.textPass)
        layout.addWidget(self.buttonLogin)

    def handleLogin(self):
        if (self.textName.text() == 'foo' and
            self.textPass.text() == 'bar'):
            self.accept()
            QtWidgets.QMessageBox.information(
                self, 'Info', 'user and password ok')
        else:
            QtWidgets.QMessageBox.warning(
                self, 'Error', 'Bad user or password')

class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    login = Login()

    if login.exec_() == QtWidgets.QDialog.Accepted:
        window = Window()
        window.show()
        sys.exit(app.exec_())
Reply
#3
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()
Reply
#4
(Dec-23-2019, 04:07 PM)Denni Wrote: # Note Never Import more than you need -- aka importing entire libraries is lazy coding
That is arguable statement at best.
It doesn't matter if you do
from PyQt import QtWidgets
or e.g.
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
in any case the whole PyQt5.QtWidgets is imported

the difference is what names are visible/accessible to you after the import and how you reference them

It's more or less a matter of preference and many prefer full references. It's more clear what comes from where.

Your statement about NOT using super() to initialize parent is also arguable.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Okay @buran first off I do stand corrected but only partially -- for importing PyQt import QtWidgets does not import the same thing as from PyQt.QtWidgets import QApplication, etc... because in the latter you have a single reference to a single method and this denotes exactly what you are using within that library within your program. This in turn makes your code more direct, less confusing and less verbose. And again not being specific is being lazy and lazy coding is always bad coding.

On the other hand -- yes the only difference between the two statements is what name is bound; import QtWidgets binds the name QtWidgets to the entire module (so QtWidgets -> QtWidgets.modules['QtWidgets']), while from QtWidgets import QApplication binds a different name, QApplication, pointing straight at the attribute contained inside of the module (so QApplication -> QtWidgets.modules['QtWidgets'].QApplication). The rest of the QtWidgets module is still there, whether you use anything else from the module or not.

Next my statement about super( ) is not arguable but go ahead and make that false claim just know that I fully researched super( ) before I made that statement -- all the pros/cons that I could find (quite numerable btw). So I am confident that I do know what it does and does not do. On the flip-side however I am guessing you do not even know why it was initially created -- aka the rare and specific issue it was created to handle -- nor are you probably aware of the issues that it creates when you do use it otherwise you would not have made your basically false claim
Reply
#6
(Dec-23-2019, 04:55 PM)Denni Wrote: Okay @buran first off importing PyQt import QtWidgets does not import the same thing as from PyQt.QtWidgets import QApplication, etc... because in the latter you have a single reference to a single method and this denotes exactly what you are using within that library within your program. And again not being specific is being lazy and lazy coding is always bad coding.
Unless PyQt is doing some weird stuff (which I doubt), it imports the whole PyQt.QtWidgets. As I said the difference is what references are available after that in the namespace.

Regarding the super() - it's "mandatory" in case of multiple inheritance. And in case of single inheritance you can do just super().__init__(*args, **kwargs), i.e. it's not required to pass explicit reference inside super(). In any case your statement that using super() is wrong is misleading and incorrect. And in the example *args, **kwargs is just that - example.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Dec-23-2019, 04:55 PM)Denni Wrote: This in turn makes your code more direct, less confusing and less verbose. And again not being specific is being lazy and lazy coding is always bad coding.
Because you edit your post - full reference what you use is just that - verbose and being very, very specific :-) And many would prefer that. I don't say what you do is wrong, just that your statements are radical and not everyone agree
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Dec-23-2019, 04:55 PM)Denni Wrote: On the other hand -- yes the only difference between the two statements is what name is bound;
And because you continue to edit your post - I am glad that now you actually agree with me :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
buran Wrote:Unless PyQt is doing some weird stuff (which I doubt), it imports the whole PyQt.QtWidgets. As I said the difference is what references are available after that in the namespace.
I believe I covered that in my update which appears to have been a cross-post situation -- and your above statement does not counter the fact that doing it as import PyQt.QtWidgets is lazy coding as opposed to from PyQt.QtWidgets import SpecificMethod and I stand on the fact that lazy coding is bad coding but it gets perpetuated a lot

buran Wrote:Regarding the super() - it's "mandatory" in case of multiple inheritance. And in case of single inheritance you can do just super().__init__(*args, **kwargs), i.e. it's not required to pass explicit reference inside super(). In any case your statement that using super() is wrong is misleading and incorrect. And in the example 8args, **kwargs is just that - example.
No it is not misleading apparently you have not bothered to fully research super( ) the rare case is just that a rather rare case and that almost every simpler program created especially by the folks that ask the simpler questions on here are never going to encounter that rare case and telling them to use super() without explaining the pitfalls is irresponsible and using it for anything but its rare case is lazy coding and thus bad coding as it adds a layer of complexity that is not needed and brings along with it the issues that get created by using super( ) Issues mind you that if you do not understand because you are not aware if its pitfalls you can easily do more easily than using the explicit method
Reply
#10
I would post this nice article by Raimond Hettinger:
Python’s super() considered super!

and not bother to answer to your false and misleading claim.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,288 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,681 May-12-2022, 07:46 PM
Last Post: Extra
  [Tkinter] Help with input from specific devices Noob_hobbyist 3 2,325 Feb-02-2021, 04:11 AM
Last Post: deanhystad
  Convert combobox user input in to date with tkinter Ame 8 6,662 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Create an identification code from user input PeroPuri 1 1,872 Apr-11-2020, 11:56 AM
Last Post: Larz60+
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,670 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,100 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 1,976 Oct-03-2019, 06:28 PM
Last Post: joe_momma
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [Tkinter] help please, checking the user input and outputting the result tomkovladko 3 2,712 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