Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

python - 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'. 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 a text method.

To answer your specific question:

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_()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...