問(wèn)題描述
我正在嘗試實(shí)現(xiàn)一個(gè)功能.我的代碼如下.
I am trying to implement a function. My code is given below.
當(dāng)用戶單擊名稱為連接"的按鈕時(shí),我想在字符串中獲取對(duì)象名稱為主機(jī)"的文本,例如主機(jī)".我怎樣才能做到這一點(diǎn)?我嘗試過(guò),但失敗了.如何實(shí)現(xiàn)這個(gè)功能?
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_()
現(xiàn)在如何實(shí)現(xiàn)button_click"功能?我剛剛開(kāi)始使用 pyQt!
Now how do I implement the function "button_click" ? I have just started with pyQt!
推薦答案
我的第一個(gè)建議是使用 Qt Designer 來(lái)創(chuàng)建你的 GUI.自己打字很糟糕,需要更多時(shí)間,而且你肯定會(huì)比 Qt Designer 犯更多的錯(cuò)誤.
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.
這里有一些 PyQt 教程 幫助您走上正軌.列表中的第一個(gè)是您應(yīng)該開(kāi)始的地方.
Here are some PyQt tutorials to help get you on the right track. The first one in the list is where you should start.
了解哪些方法可用于特定類的一個(gè)很好的指南是 PyQt4 類參考.在這種情況下,您將查找 QLineEdit
并看到有一個(gè) text
方法.
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 a text
method.
回答您的具體問(wèn)題:
要使您的 GUI 元素可用于對(duì)象的其余部分,請(qǐng)?jiān)谒鼈兦懊婕由?self.
To make your GUI elements available to the rest of the object, preface them with 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_()
這篇關(guān)于在字符串中按下 QpushButton 時(shí)如何在 QlineEdit 中獲取文本?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!