如何使用按钮从 QLineEdit() pyqt5 中获取值 [英] How to get value form QLineEdit() pyqt5 with pushing button

查看:319
本文介绍了如何使用按钮从 QLineEdit() pyqt5 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 QLineEdit 对象并试图从这个小部件中获取值,但最后我遇到了一个错误:

I have created the QLineEdit object and trying to get valuse from this widget, but at the end I've got an error:

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'

推荐答案

您的错误如下:

  • connect() 方法中,你必须输入函数的名称,而不是被评估的函数.

  • To the connect() method you must put the name of the function, not the evaluated function.

连接不返回任何内容,因此值将始终为 None.

the connection does not return anything, so value will always be None.

GUI 异步工作,因为它们要获取连接中的数据无效,必须在槽中获取.

The GUIs work asynchronously because they want to obtain the data in the connection is not valid, you must obtain it in the slot.

如果你想传递额外的参数,你可以使用 lambda 函数:

if you want to pass additional parameters you could use the lambda function:

    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)

这篇关于如何使用按钮从 QLineEdit() pyqt5 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆