Posts: 14
Threads: 5
Joined: Dec 2019
Dec-23-2019, 08:09 AM
(This post was last modified: Dec-23-2019, 08:41 AM by buran.)
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!!
Posts: 1,027
Threads: 16
Joined: Dec 2016
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_())
Posts: 211
Threads: 4
Joined: May 2019
Dec-23-2019, 04:07 PM
(This post was last modified: Dec-23-2019, 05:27 PM by Denni.)
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()
Posts: 8,156
Threads: 160
Joined: Sep 2016
Dec-23-2019, 04:38 PM
(This post was last modified: Dec-23-2019, 04:38 PM by buran.)
(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.
Posts: 211
Threads: 4
Joined: May 2019
Dec-23-2019, 04:55 PM
(This post was last modified: Dec-23-2019, 05:03 PM by Denni.)
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
Posts: 8,156
Threads: 160
Joined: Sep 2016
Dec-23-2019, 05:04 PM
(This post was last modified: Dec-23-2019, 05:04 PM by buran.)
(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.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Dec-23-2019, 05:07 PM
(This post was last modified: Dec-23-2019, 05:12 PM by buran.)
(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
Posts: 8,156
Threads: 160
Joined: Sep 2016
(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 :-)
Posts: 211
Threads: 4
Joined: May 2019
Dec-23-2019, 05:13 PM
(This post was last modified: Dec-23-2019, 05:13 PM by Denni.)
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
Posts: 8,156
Threads: 160
Joined: Sep 2016
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.
|