简体   繁体   English

如何通过按下按钮从QLineEdit()pyqt5获取值

[英]How to get value form QLineEdit() pyqt5 with pushing button

I have created the QLineEdit object and trying to get valuse from this widget, but at the end I've got an error: 我创建了QLineEdit对象,并试图从此小部件中获取值,但最后我得到了一个错误:

qlineedit1 = QLineEdit()
qlineedit1.setFixedSize(btn_x_size, btn_y_size)
gridLayout.addWidget(qlineedit1, i, j)        
value = range_btn.clicked.connect(self.get_value(qlineedit1))

@pyqtSlot()
def get_text(self, obj):
    textboxValue = obj.text()
    return textboxValue

    text1 = range_btn.clicked.connect(self.get_text(qlineedit1))
TypeError: argument 1 has unexpected type 'str'

Your errors are the following: 您的错误如下:

  • To the connect() method you must put the name of the function, not the evaluated function. 对于connect()方法,您必须输入函数名称,而不是评估函数。

  • the connection does not return anything, so value will always be None. 连接不会返回任何内容,因此value始终为None。

The GUI s work asynchronously because they want to obtain the data in the connection is not valid, you must obtain it in the slot. GUI异步工作,因为它们想要获取连接中无效的数据,因此必须在插槽中获取它。

if you want to pass additional parameters you could use the lambda function: 如果要传递其他参数,则可以使用lambda函数:

    qlineedit1 = QLineEdit()
    qlineedit1.setFixedSize(btn_x_size, btn_y_size)
    gridLayout.addWidget(qlineedit1, i, j)        
    range_btn.clicked.connect(lambda checked, obj=qlineedit1 : self.get_value(obj))

@pyqtSlot()
def get_text(self, obj):
    textboxValue = obj.text()
    print(textboxValue)

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

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