Reputation: 549
I have a form with 2 widgets. One LineEdit
(name : lineEdit
) and Button
(name pushButton_2
).
Below my code:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(140, 70, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(130, 10, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), myFunc)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton_2.setText(_translate("Form", "Press me!", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I want make a function so I press button
it will get value text form LineEdit
widget. Like the following:
def myfunc():
text=Get_Value_From_Widget()
...........
return text
How can I do it?
Upvotes: 0
Views: 521
Reputation: 5336
Alternatively, you can code this simple widget manually, without using QtDesigner. It may be a little harder at first, but it will be worth it in the long run, as you gain a much finer control on what you do.
Here is a simple example of what you may want to do.
from PyQt4 import QtGui, QtCore
class CustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(QtGui.QWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
self.lineEdit1 = QtGui.QLineEdit(self)
layout.addWidget(self.lineEdit1)
self.pushButton = QtGui.QPushButton("line1 -> line2", self)
self.pushButton.clicked.connect(self.onClick)
layout.addWidget(self.pushButton)
self.lineEdit2 = QtGui.QLineEdit(self)
layout.addWidget(self.lineEdit2)
def onClick(self):
self.lineEdit2.setText(self.lineEdit1.text())
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
widget = CustomWidget()
widget.show()
sys.exit(app.exec_())
Upvotes: 1
Reputation: 3297
First of all, I think you should look at the Qt documentation about SIGNAL/SLOT mechanism.
Anyway, your function should be defined into the Ui_Form
class as
def myFunc(self):
text=self.lineEdit.text()
return text
Please note that the code is case sensitive (as the name of the methods/functions) so in your case myFunc
is not the same method as myfunc
Upvotes: 0