[英]How to get text in QlineEdit when QpushButton is pressed in a string?
I am trying to implement a function.我正在尝试实现一个功能。 My code is given below.我的代码如下。
I want to get the text in lineedit with objectname 'host' in a string say 'shost' when the user clicks the pushbutton with name 'connect'.当用户单击名称为“connect”的按钮时,我想在字符串中使用 objectname 'host' 获取 lineedit 中的文本,例如 'shost'。 How can I do this?我怎样才能做到这一点? I tried and failed.我尝试过但失败了。 How do I implement this function?我如何实现这个功能?
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_()
Now how do I implement the function "button_click" ?现在如何实现“button_click”功能? I have just started with pyQt!我刚刚开始使用 pyQt!
My first suggestion is to use Qt Designer to create your GUIs.我的第一个建议是使用 Qt Designer 来创建您的 GUI。 Typing them out yourself sucks, takes more time, and you will definitely make more mistakes than Qt Designer.自己输入它们很糟糕,需要更多时间,而且你肯定会比 Qt Designer 犯更多的错误。
Here are some PyQt tutorials to help get you on the right track.这里有一些PyQt 教程可以帮助您走上正轨。 The first one in the list is where you should start.列表中的第一个是您应该开始的地方。
A good guide for figuring out what methods are available for specific classes is the PyQt4 Class Reference . PyQt4 Class Reference是确定特定类可用哪些方法的一个很好的指南。 In this case, you would look up QLineEdit
and see the there is a text
method.在这种情况下,您将查找QLineEdit
并看到有一个text
方法。
To answer your specific question:要回答您的具体问题:
To make your GUI elements available to the rest of the object, preface them with self.
要使您的 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_()
The object name is not very important.对象名称不是很重要。 what you should be focusing at is the variable that stores the lineedit object (le) and your pushbutton object(pb)您应该关注的是存储 lineedit 对象 (le) 和您的按钮对象 (pb) 的变量
QObject(self.pb, SIGNAL("clicked()"), self.button_clicked) def button_clicked(self): self.le.setText("shost")
I think this is what you want.我想这就是你想要的。 I hope i got your question correctly :)我希望我正确回答了你的问题:)
Acepted solution implemented in PyQt5在 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_()
Short and general answer is:简短而笼统的答案是:
self.input = QLineEdit()
your_text = self.input.text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.