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?
#11
buran Wrote:I would post this nice article by Raimond Hettinger:
And I would post this nice article by James Knight just one of a few posts outlining the issues with Super()
Python's Super is nifty, but you can't use it

Let me also include this counter point post made to someone else's declaration that Super() is good
Quote:If you do use super, here are some best practices:
* Use it consistently, and document that you use it, as it is part of the external interface for your class, like it or not.
* Never call super with anything but the exact arguments you received, unless you really know what you're doing.
* When you use it on methods whose acceptable arguments can be altered on a subclass via addition of more optional arguments, always accept *args, **kw, and call super like "super(MyClass, self).currentmethod(alltheargsideclared, *args, **kwargs)". If you don't do this, forbid addition of optional arguments in subclasses.
* Never use positional arguments in __init__ or __new__. Always use keyword args, and always call them as keywords, and always pass all keywords on to super.

From my experience in python code, needing to define the same name method in two branches of an inheritance tree is exceedingly rare, with the exclusion of __init__. And, super() is useless for that if you need any kind of interoperability with existing python code. I cannot envision any implementation where this functionality would actually be needed.
The key I would point out here is this final statement which after fully investigated super() I fully agree with I cannot envision any implementation where this functionality would actually be needed. Or to state it another way... unless you are implementing poorly designed code due to lazy thinking there should really be no need for its use under most circumstances.


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.
buran Wrote:Because you edit your post - full reference what you use is just that - verbose and being very, very specific :-)
Okay this makes no sense -- but I think I got it maybe -- the verbosity I am speaking of is this one
PyQt.QtWidgets.QWidget.method()
versus the same line of code written as follows:
QWidget.method()

The extra PyQt.QtWidgets. which is not needed if done succinctly only clutters up the code especially when you start referencing more than one such item on a single line such as in making calls to
PyQt.QtCore.Qt.Something,PyQt.QtCore.Qt.Something, etc...
instead just referencing
Qt.Something,Qt.Something,etc...
which is not all that uncommon of a thing to need to do

buran Wrote:And many would prefer that. I don't say what you do is wrong, just that your statements are radical and not everyone agree
Okay I am not sure who these fictitious "many" are and further you cannot state anything that I have said is wrong because frankly it is not wrong at all and I have been pointing just how wrong your statements have been. Further claiming my statements are radical is also wrong as they are not radical at all they are held by many high-quality software engineers as opposed to the opinions of numerous programmer wanna-bes and lazy coders -- of that entire statement the only thing that had a spec of truth in it was not everyone agree which should have been pluralized -- but just because folks do not agree with this does not make it no less true -- folks used to all agree the world was flat but just because they all agreed about this false claim it did not make it true and this goes for many many things many folks seem to "agree" upon -- I say think for yourself and know for yourself. I am perfectly comfortable with anyone fully researching these topics, nay I encourage folks to do so, and if I am wrong and you can prove it please do so but so far that is not the case.


Denni Wrote:On the other hand -- yes the only difference between the two statements is what name is bound;
buran Wrote:And because you continue to edit your post - I am glad that now you actually agree with me :-)
First the only continuing editing I have done past the first edit -- which I denoted was a cross-posting issue -- have been typo fixes.

Next I do not agree with you fully only in part as I denoted. So because you chose to take my words out of context I will quote myself from that same post to remind you and anyone else reading this about your fopaux

Me form that same post Wrote:...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.
Reply
#12
(Dec-23-2019, 06:17 PM)Denni Wrote: First the only continuing editing I have done past the first edit -- which I denoted was a cross-posting issue -- have been typo fixes.
do you want me to revert your edits to your original post? currently your post in question is edited 4 times.


Denni, original post 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 case only the elements of the QtWidgets library that are associated with the named objects and the objects those objects inherit from are going to be imported which is definitely NOT the entirety of the QtWidgets library so your statement is false --- however I will state I base this off the normal rules of importing which python follows so unless PyQt is breaking this basic rule then please point to where they state this is actually the case -- otherwise you have no foundation to stand on

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

skip first edit - looks like just typos

Denni, second edit 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



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

skip third edit - again looks like just typos

Denni, fourth edit, content at the moment Wrote: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


As all can see the major edits, not typos, are in second edit and fourth edit.
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
#13
@YoshikageKira, sorry for hijacking your thread for what is more like general discussion, not just answering your question.
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
#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
#15
buran Wrote:do you want me to revert your edits to your original post? currently your post in question is edited 4 times.
Yep @buran I made that 2nd change and then I made that 4th change but I believe I made the note that I made an edit after that 4th change when I saw you had replied while I was making my edits. Of course all of those edits were done I think fairly quickly in succession so the timing of it all might be slightly off. Still I do not see your point? As you quoted my 4th change in your reply to my edit while leaving out the reference to the 2nd change. So you purposefully chose to pull something out of context and misrepresent what I said. Nice and no apology to go with it, typical.

@YoshikageKira as for saving that information once you have gotten it into a variable state you can save that virtually anywhere you want to however, a database is typically used in this case and python incorporates sqlite3 which is a very easy and free database engine to use however when doing this you are going to want to use MVC methodology and if you do not know about MVC you should read up on that. Regardless if you want to learn this kind of stuff interactively you can join my free python-pyqt classroom or I can continue helping you here -- just post another question on this once you have looked into MVC and using sqlite3 with python --- note do not look into PyQt inter-activeness with a database as that is not MVC and frankly straight python works better for a database interface

Still if you did not want to go the route of a database MVC is still a methodology you want to investigate and then you could save into a text file or Jason object within a text file
Reply
#16
(Dec-23-2019, 10:33 PM)Denni Wrote:
buran Wrote:do you want me to revert your edits to your original post? currently your post in question is edited 4 times.
Yep @buran I made that 2nd change and then I made that 4th change but I believe I made the note that I made an edit after that 4th change when I saw you had replied while I was making my edits. Of course all of those edits were done I think fairly quickly in succession so the timing of it all might be slightly off. Still I do not see your point? As you quoted my 4th change in your reply to my edit while leaving out the reference to the 2nd change. So you purposefully chose to pull something out of context and misrepresent what I said. Nice and no apology to go with it, typical.

@YoshikageKira as for saving that information once you have gotten it into a variable state you can save that virtually anywhere you want to however, a database is typically used in this case and python incorporates sqlite3 which is a very easy and free database engine to use however when doing this you are going to want to use MVC methodology and if you do not know about MVC you should read up on that. Regardless if you want to learn this kind of stuff interactively you can join my free python-pyqt classroom or I can continue helping you here -- just post another question on this once you have looked into MVC and using sqlite3 with python --- note do not look into PyQt inter-activeness with a database as that is not MVC and frankly straight python works better for a database interface

Still if you did not want to go the route of a database MVC is still a methodology you want to investigate and then you could save into a text file or Jason object within a text file

What a coincidence because I just so happen to be experimenting with sqlite3 and I am attempting to use it with my project. It would be perfect to learn the MVC to use with my project because I am attempting to have a program that allows you log information (integer related such as Volunteer hours) into a database and calculate certain information etc. Tho I do perfer to be helped here, I would love to know the code to doing would be. I could a send a massive snipet of the code that Im trying to incorporate the method into.
Reply
#17
(Dec-23-2019, 10:33 PM)Denni Wrote:
buran Wrote:do you want me to revert your edits to your original post? currently your post in question is edited 4 times.
Yep @buran I made that 2nd change and then I made that 4th change but I believe I made the note that I made an edit after that 4th change when I saw you had replied while I was making my edits. Of course all of those edits were done I think fairly quickly in succession so the timing of it all might be slightly off. Still I do not see your point? As you quoted my 4th change in your reply to my edit while leaving out the reference to the 2nd change. So you purposefully chose to pull something out of context and misrepresent what I said. Nice and no apology to go with it, typical.

@YoshikageKira as for saving that information once you have gotten it into a variable state you can save that virtually anywhere you want to however, a database is typically used in this case and python incorporates sqlite3 which is a very easy and free database engine to use however when doing this you are going to want to use MVC methodology and if you do not know about MVC you should read up on that. Regardless if you want to learn this kind of stuff interactively you can join my free python-pyqt classroom or I can continue helping you here -- just post another question on this once you have looked into MVC and using sqlite3 with python --- note do not look into PyQt inter-activeness with a database as that is not MVC and frankly straight python works better for a database interface

Still if you did not want to go the route of a database MVC is still a methodology you want to investigate and then you could save into a text file or Jason object within a text file

Do you know where I can find your python pyqt class? I would love to learn how to incorporate the sqlite3 database into my program
Reply
#18
@YoshikageKira yes I know where the pyqt sql class stuff is but I would strongly suggest that you do not use it especially if you are going to use sqlite3 as that is native to python and using MVC methodology you would want to build that Database Class as a python only self-contained autonomous class that takes dictionary parameters and passes back dictionary recordsets. I have a template of this that I share (for free) with my students and I can share that with you as well if you are interested.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,282 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,679 May-12-2022, 07:46 PM
Last Post: Extra
  [Tkinter] Help with input from specific devices Noob_hobbyist 3 2,324 Feb-02-2021, 04:11 AM
Last Post: deanhystad
  Convert combobox user input in to date with tkinter Ame 8 6,656 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Create an identification code from user input PeroPuri 1 1,870 Apr-11-2020, 11:56 AM
Last Post: Larz60+
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,661 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,097 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,971 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,710 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