简体   繁体   English

使用PySide / PyQt将字符串分配给QLineEdit

[英]Assign string to QLineEdit with PySide/PyQt

I'm having a little bit of trouble assigning values to a QLineEdit. 将值分配给QLineEdit时有点麻烦。 I've read the documentation and feel that the QLineEdit.SetText() command will be used at some point. 我已经阅读了文档,并觉得QLineEdit.SetText()命令将在某个时候使用。

I've used Qt Designer to design a GUI for my software. 我已经使用Qt Designer为我的软件设计GUI。 On the main window (MainWindow.py, with an accompanying ui_MainWindow.py setup file), I have a LineEdit (lineEditScanBarcode) which has strong focus. 在主窗口(MainWindow.py,以及随附的ui_MainWindow.py设置文件)上,我有一个LineEdit(lineEditScanBarcode),它具有很强的焦点。 I've managed to pull input from that LineEdit pretty well. 我已经设法很好地从LineEdit中提取输入。 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. 如果LineEditScanBarcode = x中的输入,则将名称“ John Smith”分配给具有零焦点策略的辅助QLineEdit(lineEditUser)。 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! 我仍然不知道要在另一个窗口上打开一个窗口(此软件包将有大约10个窗口),但这是另一回事了!

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. 我以前从未使用过Qt,因此至少可以说我对所涉及的复杂性缺乏了解。

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). 您正在尝试将值'John'分配给该函数调用(作为错误状态)。 If you review the documentation for QLineEdit in PyQT , you'll see that QLineEdit.setText() requires a string to be passed to it. 如果查看PyQT中QLineEdit的文档,您会看到QLineEdit.setText()需要将字符串传递给它。

So, what you need to do instead is pass the value 'John' to the function like so: 因此,您需要做的是将值'John'传递给函数,如下所示:

self.lineEditUser.setText('John')

On another note your idea that your 另一方面,您的想法是

software package will have about 10 windows 软件包大约有10个窗口

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 . 我强烈建议您在UserExperience.SE上分享您的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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