Python Forum
recording textbox data into a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recording textbox data into a variable
#1
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'
Reply
#2
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
Reply
#3
None of the 2 solutions work for me when line 44 is uncommented; I'm missing something for sure.
Reply
#4
Line 44 is needed for what?
Reply
#5
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 2,235 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  [Tkinter] Redirecting stdout to TextBox in realtime Gilush 2 10,009 Jun-06-2020, 11:05 AM
Last Post: deanhystad
  tkinter how to unselect text in textbox Battant 0 2,313 Feb-17-2020, 02:00 PM
Last Post: Battant
  [Tkinter] cannot type in textbox msteffes 2 3,070 Jul-28-2018, 04:20 AM
Last Post: msteffes
  GUI insert textbox value into a Class dimvord 0 2,299 Jul-04-2018, 06:49 PM
Last Post: dimvord

Forum Jump:

User Panel Messages

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