当在字符串中按下 QpushButton 时,如何在 QlineEdit 中获取文本?
我正在尝试实现一个功能。我的代码如下。
当用户单击名称为“connect”的按钮时,我想在 lineedit 中获取对象名为“host”的文本,字符串中为“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!
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'. 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" ? I have just started with pyQt!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的第一个建议是使用 Qt Designer 来创建 GUI。自己输入它们很糟糕,需要更多时间,而且肯定会比 Qt Designer 犯更多错误。
这里有一些 PyQt 教程来帮助您让你走上正轨。列表中的第一个是您应该开始的地方。
要了解哪些方法可用于特定类,一个很好的指南是 PyQt4 类参考。在这种情况下,您将查找
QLineEdit
并看到有一个text
方法。要回答您的具体问题:
要使您的 GUI 元素可供对象的其余部分使用,请在它们前面加上
self.
My first suggestion is to use Qt Designer to create your GUIs. Typing them out yourself sucks, takes more time, and you will definitely make more mistakes than Qt Designer.
Here are some PyQt tutorials to help get you on the right track. 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. In this case, you would look up
QLineEdit
and see the there is atext
method.To answer your specific question:
To make your GUI elements available to the rest of the object, preface them with
self.
对象名称并不是很重要。
您应该关注的是存储 lineedit 对象(le)和按钮对象(pb)的变量,
我认为这就是您想要的。
我希望我正确地回答了你的问题:)
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)
I think this is what you want.
I hope i got your question correctly :)
在 PyQt5 中实现的可接受的解决方案
Acepted solution implemented in PyQt5
简短而一般的答案是:
Short and general answer is: