PythonNLMB
PythonNLMB

Reputation: 41

How to print input of QLineEdit/ store in variable?

How do I print the input of the QLineEdit? or store as it a variable for use in a function later?

Here is what i've tried:

self.QLineEdit.text()

My full code is really messy, if anybody has an example somewhere, I would really appreciate it. My code is returning no text.

Upvotes: 0

Views: 7876

Answers (2)

VdF
VdF

Reputation: 49

As mentionned in @Andrew Paxson's answer, you cannot use self.QLineEdit. You must have an instance of it in your window :

self.line_edit = QtGui.QLineEdit() 

then you can use self.line_edit.text to store or print its value.

Upvotes: 1

Andrew Paxson
Andrew Paxson

Reputation: 305

Not Sure what exactly you mean but here is an example of the QLineEdit in a class showing how you can return a value and store it.

from PyQt import QtGui


class SimpleExample(object):

    def __init__(self):

        # instance line edit
        self.line_edit = QtGui.QLineEdit()

    def pretend_something_happened(self):
        # User Did something
        self.line_edit.setText("User Entered Something")

    def line_value(self):
        # return text value of line edit
        return self.line_edit.text()


if __name__ == '__main__':
    s = SimpleExample()

    # Store Value
    val = s.line_value()
    print(val)

    s.pretend_something_happened()

    # Print Value to show the value was copied
    print(val)


    # Store new value
    val = s.line_value()
    print(val)

Upvotes: 1

Related Questions