简体   繁体   中英

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM