简体   繁体   中英

Assign string to QLineEdit with PySide/PyQt

I'm having a little bit of trouble assigning values to a QLineEdit. I've read the documentation and feel that the QLineEdit.SetText() command will be used at some point.

I've used Qt Designer to design a GUI for my software. On the main window (MainWindow.py, with an accompanying ui_MainWindow.py setup file), I have a LineEdit (lineEditScanBarcode) which has strong focus. I've managed to pull input from that LineEdit pretty well. What I'd like to do is this:

If the input in LineEditScanBarcode = x, then assign the name 'John Smith' to a secondary QLineEdit (lineEditUser) which has a zero focus policy. This is what I have so far:

def ScanBarcode(self):
    barcode = self.lineEditScanBarcode.text()
    self.lineEditScanBarcode.clear()
    if barcode == '12345':
        print("Welcome John")
        self.lineEditUser.setText() = 'John'
    else: print("Sorry, user not recognised.")

Upon running this, I get the following error:

Syntax Error: can't assign to function call

I've had a look at the above error, but I'm still unsure as to what's going on here. I still have no idea to open one window on top of another (this software package will have about 10 windows), but that's another story!

Is my logic here on track? I've never used Qt before, so my understanding of the intricacies involved is lacking to say the least.

Any input would be great!

As the comment states, the error is on this line:

self.lineEditUser.setText() = 'John'

You are attempting to assign the value 'John' to that functioncall (as the error states). If you review the documentation for QLineEdit in PyQT , you'll see that QLineEdit.setText() requires a string to be passed to it.

So, what you need to do instead is pass the value 'John' to the function like so:

self.lineEditUser.setText('John')

On another note your idea that your

software package will have about 10 windows

is definitely something that you want to reexamine. More windows, especially when undocked and floating independently will no doubt cause usability issues. I'd strongly recommend sharing your ideas over at UserExperience.SE .

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