当按下 QpushButton 后如何以字符串形式获取 QlineEdit 中的文本?

57

我正在尝试实现一个函数,我的代码如下:

当用户点击名称为“connect”的按钮时,我想要获取带有对象名称'host'的lineedit中的文本字符串,并将其存储在名为'shost'的字符串变量中。我曾经尝试过但失败了,请问如何实现这个功能?

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        le = QLineEdit()
        le.setObjectName("host")
        le.setText("Host")
        pb = QPushButton()
        pb.setObjectName("connect")
        pb.setText("Connect") 
        layout.addWidget(le)
        layout.addWidget(pb)
        self.setLayout(layout)

        self.connect(pb, SIGNAL("clicked()"),self.button_click)

        self.setWindowTitle("Learning")

    def button_click(self):
    #i want the text in lineedit with objectname 
    #'host' in a string say 'shost'. when the user click 
    # the pushbutton with name connect.How do i do it?
    # I tried and failed. How to implement this function?




app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

现在我该如何实现函数"button_click"?我刚开始使用pyQt!

4个回答

67

我的第一个建议是使用Qt Designer创建GUI。自己打比较麻烦,需要更多时间,并且肯定会比Qt Designer做得更差。

这里有一些PyQt教程可以帮助你入门。列表中的第一个是你应该开始学习的。

查找特定类别可用方法的好指南是PyQt4类参考文档。在这种情况下,您将查找QLineEdit并查看是否有text方法。

回答你具体的问题:

为了使GUI元素对对象的其他部分可用,请在它们之前加上self.

import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.le = QLineEdit()
        self.le.setObjectName("host")
        self.le.setText("Host")
        
        self.pb = QPushButton()
        self.pb.setObjectName("connect")
        self.pb.setText("Connect") 
        
        layout = QFormLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)

        self.setLayout(layout)
        self.connect(self.pb, SIGNAL("clicked()"),self.button_click)
        self.setWindowTitle("Learning")

    def button_click(self):
        # shost is a QString object
        shost = self.le.text()
        print shost
        

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

2
你需要在__init__函数中将它命名为self.le。在你的Form类中使用时,每个地方都应该是self.le - tgray
当我运行以上代码时,没有出现错误。我使用的是Python 2.6版本,你用的是哪个版本? - tgray
Python 2.6.5 在 Ubuntu 64 位系统下。 - esafwan
它起作用了!谢谢。有一个语法错误。非常感谢您! - esafwan
2
shost = self.le.text() 很可能会得到一个 QString 输出。使用 str() 进行转换,以获得有意义的文本。 - Arindam Roychowdhury
显示剩余2条评论

2

对象名称并不是很重要。 你应该关注存储LineEdit对象(le)和PushButton对象(pb)的变量。

QObject(self.pb, SIGNAL("clicked()"), self.button_clicked)
def button_clicked(self): self.le.setText("shost")

我认为这就是你想要的。 希望我正确理解了你的问题 :)


2

在PyQt5中实现的已接受解决方案

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QFormLayout
from PyQt5.QtWidgets import (QPushButton, QLineEdit)

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.le = QLineEdit()
        self.le.setObjectName("host")
        self.le.setText("Host")

        self.pb = QPushButton()
        self.pb.setObjectName("connect")
        self.pb.setText("Connect")
        self.pb.clicked.connect(self.button_click)

        layout = QFormLayout()
        layout.addWidget(self.le)
        layout.addWidget(self.pb)
        self.setLayout(layout)

        self.setWindowTitle("Learning")

    def button_click(self):
        # shost is a QString object
        shost = self.le.text()
        print (shost)


app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

0

简短而通俗的回答是:

self.input = QLineEdit()
your_text = self.input.text()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接