views:

460

answers:

2

I have given my code below, have problem in implementing a function

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?

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 "def button_click(self):" ? I have just started with pyQt!

+1  A: 

My first suggestion is to use Designer to create your GUIs. Typing them out yourself sucks, takes more time, and you will definitely make more mistakes than 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_()
tgray
I'm getting this error! AttributeError: 'Form' object has no attribute 'le'
esafwan
You need to name it `self.le` in your `__init__` function. Everywhere you use it in your `Form` class it should be `self.le`.
tgray
I didn't get you. Sorry, I'm just getting started with these... i used the code which you gave, and the error came. I think in it, its already self.le
esafwan
I don't get the error when I run the above code. I'm using Python 2.6, what version are you using?
tgray
Python 2.6.5 under ubuntu 64 bit.
esafwan
It worked! Thanx. There was a syntax error. Thank You Very Much!
esafwan
A: 

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)

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 :)

lionel319