Python Forum
PyQt4 get text from line edit into label on button push
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt4 get text from line edit into label on button push
#1
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
Reply
#2
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()
Reply
#3
Thank you, it was so simple I didn't think to try it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 827 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,702 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,777 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  Can't change the colour of Tk button text Pilover 6 14,707 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,882 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,831 Jun-26-2022, 06:26 PM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,080 May-31-2022, 06:37 PM
Last Post: Winfried
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,728 May-12-2022, 07:46 PM
Last Post: Extra
  [Tkinter] Make my button text update? Skata100 1 2,030 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 2,231 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020