PyQt4 get text from line edit into label on button push - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: PyQt4 get text from line edit into label on button push (/thread-2183.html) |
PyQt4 get text from line edit into label on button push - iFunKtion - Feb-24-2017 I am trying to design data input form that will add text to a label when the text is entered into a line edit widget when the ok button is pressed, but the answer is completely eluding me: #!/usr/bin/python3 #-*- coding: utf-8 -*- """ Set label text from line edit with ok click """ import sys from PyQt4 import QtGui, QtCore class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.qle = QtGui.QLineEdit(self) self.qle.move(100, 0) global sometext sometext = self.qle.text # <---- This line I think is the problem self.lbl = QtGui.QLabel(self) self.lbl.move(100, 100) btn = QtGui.QPushButton("Ok", self) btn.move(30, 100) btn.clicked.connect(self.buttonClicked) self.setGeometry(200, 200, 300, 200) self.show def buttonClicked(self, sometext): sender = self.sender() self.lbl.setText(sometext) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()How do I make this work please RE: PyQt4 get text from line edit into label on button push - Raures - Feb-25-2017 You could simply call QLineEdit's .text() method. Solution below. #!/usr/bin/python3 #-*- coding: utf-8 -*- """ Set label text from line edit with ok click """ import sys from PyQt4 import QtGui, QtCore class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.qle = QtGui.QLineEdit(self) self.qle.move(5, 5) # re global sometext sometext = self.qle.text # <---- This line I think is the problem self.lbl = QtGui.QLabel(self) self.lbl.move(5, 55) btn = QtGui.QPushButton("Ok", self) btn.move(5, 30) btn.clicked.connect(self.buttonClicked) self.setGeometry(200, 200, 300, 200) self.show() def buttonClicked(self, sometext): sender = self.sender() self.lbl.setText(self.qle.text()) # calling .text() method to # get the text from QLineEdit def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() RE: PyQt4 get text from line edit into label on button push - iFunKtion - Feb-27-2017 Thank you, it was so simple I didn't think to try it. |