【问题标题】:How do I access the value from a QLineEdit?如何从 QLineEdit 访问值?
【发布时间】:2021-01-20 02:07:23
【问题描述】:

如何获取和存储写入 QLineEdit 中的值以及单击保存后关闭小部件?我查看了 PyQt5 文档,它说函数 .text() 将允许访问 QLineEdit 中的值,但由于某种原因,我不断获得的值是默认的空字符串。

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")

【问题讨论】:

    标签: python json python-3.x user-interface pyqt5


    【解决方案1】:

    问题是你在构建窗口时获取文本,所以用户无法与GUI交互,而是必须在clickedSave中获取信息。

    另一方面,我看到这段代码永远无法执行(这与失败不同),因为除了类似的错误(错别字)之外,没有 QLineEdit 具有“ui”属性,所以请尝试提供更好的代码下次再来。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      相关资源
      最近更新 更多