Python Forum
recording textbox data into a variable - 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: recording textbox data into a variable (/thread-41627.html)



recording textbox data into a variable - paul18fr - Feb-19-2024

Hi

This is my very (very) first test to code a GUI and I know it'll be a very (very) long way Wink .

In the following example, printing textbox content works, but I do not figured out how to record it into a variable; I'm persuaded I'm missed many things and I'm lost in the huge documentations: links and advices are highly apprciated.

Paul

import sys
from PySide6.QtWidgets import QMainWindow, QApplication, QWidget, QLineEdit
from PySide6.QtCore import Slot



class MyMainWindow(QMainWindow):
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Qt window")
        self.setMinimumSize(800,600)

        # widget of central area
        centralArea = QWidget()
        centralArea.setStyleSheet("background: #5A5E6B")
        self.setCentralWidget(centralArea) 

        # textbox implementation
        textBox1 = QLineEdit("", centralArea)
        textBox1.setStyleSheet('background: lightgrey;'
                               'color: red;')
        textBox1.setGeometry(200, 90, 270, 30)
        textBox1.textEdited.connect(self.getTextInTextBox)              # signal = "textEdited"
                                                                        # my own slot = "getTextInTextBox"

    # slots definition
    @Slot(str)
    def getTextInTextBox(self, txt: str):          
        textRecovering: QLineEdit  = self.sender()
        print(f"Text in the textbox is '{textRecovering.text()}'")
        return textRecovering.text()

        
    # should i define a setter / getter ?


if __name__ == "__main__":
    
    app = QApplication(sys.argv)    

    MyWindow = MyMainWindow()
    MyWindow.show()
    #implementedText = MyWindow .getTextInTextBox()

    sys.exit(app.exec())
Error:
TypeError: MyMainWindow.getTextInTextBox() missing 2 required positional arguments: 'self' and 'txt'



RE: recording textbox data into a variable - Axel_Erfurt - Feb-19-2024

That should be enough.

    def getTextInTextBox(self):          
        textRecovering = self.sender()
        print(f"Text in the textbox is '{textRecovering.text()}'")
        return textRecovering.text()
or (tested in PyQt6)

    def getTextInTextBox(self, text):
        print(f"Text in the textbox is '{text}'")
        return text



RE: recording textbox data into a variable - paul18fr - Feb-19-2024

None of the 2 solutions work for me when line 44 is uncommented; I'm missing something for sure.


RE: recording textbox data into a variable - Axel_Erfurt - Feb-19-2024

Line 44 is needed for what?


RE: recording textbox data into a variable - Axel_Erfurt - Feb-19-2024

That works with PySide6

import sys
from PySide6.QtWidgets import QMainWindow, QApplication, QWidget, QLineEdit
from PySide6.QtCore import Slot
 
 
 
class MyMainWindow(QMainWindow):
     
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Qt window")
        self.setMinimumSize(800,600)
 
        # widget of central area
        centralArea = QWidget()
        centralArea.setStyleSheet("background: #5A5E6B")
        self.setCentralWidget(centralArea) 
 
        # textbox implementation
        textBox1 = QLineEdit("", centralArea)
        textBox1.setStyleSheet('background: lightgrey;'
                               'color: red;')
        textBox1.setGeometry(200, 90, 270, 30)
        textBox1.textEdited.connect(self.getTextInTextBox)              # signal = "textEdited"
                                                                        # my own slot = "getTextInTextBox"
 
    # slots definition
    @Slot(str)
    def getTextInTextBox(self, text):          
        print(f"Text in the textbox is '{text}'")
        return text
 
 
if __name__ == "__main__":
     
    app = QApplication(sys.argv)    
 
    MyWindow = MyMainWindow()
    MyWindow.show()
    implementedText = MyWindow.getTextInTextBox("")
 
    sys.exit(app.exec())