Skip to content
Advertisement

How do I access the value from a QLineEdit?

How do I get and store the value that is written in a QLineEdit as well as close the widget after I click save? I have looked over the PyQt5 documentation and it says the function .text() will give access to the values in a QLineEdit but for some reason the value I keep getting is the default empty string.

class ImageSettingsWindow(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.setWindowTitle("Image Settings")
        self.setFixedWidth(250)
        self.setFixedHeight(300)

        allImg_checkBox = QCheckBox("All Images")
        lastImg_checkBox = QCheckBox("Last Image")
        timedImgs_checkBox = QCheckBox("Timed Images")
        start_label = QLabel("Start Duration:")
        txt_start = QLineEdit()
        self.data_start = txt_start.ui.text()
        end_label = QLabel("End Duration:")
        txt_end = QLineEdit()
        self.data_end = txt_end.ui.text()
        timeInt_label = QLabel("Time Interval:")
        txt_timeInt = QLineEdit()
        self.data_timeint = txt_timeInt.ui.text()
        maxDiff_label = QLabel("Max Difference:")
        txt_maxDiff = QLineEdit()
        self.data_maxdiff = txt_maxDiff.ui.txt()
        buttonSave = QPushButton("Save")

        layout.addWidget(allImg_checkBox)
        layout.addWidget(lastImg_checkBox)
        layout.addWidget(timedImgs_checkBox)
        layout.addWidget(start_label)
        layout.addWidget(txt_start)
        layout.addWidget(end_label)
        layout.addWidget(txt_end)
        layout.addWidget(timeInt_label)
        layout.addWidget(txt_timeInt)
        layout.addWidget(maxDiff_label)
        layout.addWidget(txt_maxDiff)
        layout.addWidget(buttonSave)

    
        print(self.data_start)
        self.setLayout(layout)

        buttonSave.clicked.connect(self.clickedSave)

    

    def clickedSave(self):
        #data_start = self.
        print("button")
        try:
            json_image = open("config/image.json","r")
            param = json.load(json_image)
            json_image.close()
            param["start_duration"] = int(self.data_start)
            param["end_duration"] = int(self.data_end)
            param["time_interval"] = float(self.data_timeint)
            param["max_diff"] = int(self.data_maxdiff)

            json_image = open("config/image.json", "w")
            json.dump(param,json_image)
            json_image.close()

        except IOError as err :
            print("Error writing the image.json file , please double check")

Advertisement

Answer

The problem is that you are obtaining the text when the window is being built, so the user cannot interact with the GUI, instead you must obtain the information within clickedSave.

On the other hand, I see that this code can never be executed (which is different from failing) since no QLineEdit has a “ui” attribute in addition to similar errors(typos), so please try to provide a better code for the next time.

class ImageSettingsWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Image Settings")
        self.setFixedSize(250, 300)

        allImg_checkBox = QCheckBox("All Images")
        lastImg_checkBox = QCheckBox("Last Image")
        timedImgs_checkBox = QCheckBox("Timed Images")
        start_label = QLabel("Start Duration:")
        self.txt_start = QLineEdit()
        end_label = QLabel("End Duration:")
        self.txt_end = QLineEdit()
        timeInt_label = QLabel("Time Interval:")
        self.txt_timeInt = QLineEdit()
        maxDiff_label = QLabel("Max Difference:")
        self.txt_maxDiff = QLineEdit()
        buttonSave = QPushButton("Save")

        layout = QVBoxLayout(self)
        layout.addWidget(allImg_checkBox)
        layout.addWidget(lastImg_checkBox)
        layout.addWidget(timedImgs_checkBox)
        layout.addWidget(start_label)
        layout.addWidget(self.txt_start)
        layout.addWidget(end_label)
        layout.addWidget(self.txt_end)
        layout.addWidget(timeInt_label)
        layout.addWidget(self.txt_timeInt)
        layout.addWidget(maxDiff_label)
        layout.addWidget(self.txt_maxDiff)
        layout.addWidget(buttonSave)

        buttonSave.clicked.connect(self.clickedSave)

    def clickedSave(self):
        try:
            param = dict()
            with open("config/image.json", "r") as json_image:
                param = json.load(json_image)

            param["start_duration"] = int(self.txt_start.text())
            param["end_duration"] = int(self.txt_end.text())
            param["time_interval"] = float(self.txt_timeInt.text())
            param["max_diff"] = int(self.txt_maxDiff.text())

            with open("config/image.json", "w") as json_image:
                json.dump(param, json_image)

        except IOError as err:
            print("Error writing the image.json file , please double check", err)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement