Python Forum

Full Version: recording textbox data into a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'
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
None of the 2 solutions work for me when line 44 is uncommented; I'm missing something for sure.
Line 44 is needed for what?
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())