Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I set the value of the textedit box and slider of ui with the value from a config file when it has been created



  • Hi

    How can I set the value of the textedit box and slider of ui with the value from a config file when it has been created meaning if a configuration file exists then set the UI with value from the config file otherwise load ui with nothing set to any value.

    ui.py

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QSlider, QLineEdit, QPushButton
    from PyQt5.QtCore import Qt
    import myconfig
    config=myconfig.Config()
    import os
    import configparser
    
    class Example(QMainWindow):
    
        def __init__(self):
            super().__init__()
    
            self.textbox = QLineEdit(self)
            self.textbox.move(20, 25)
            self.textbox.resize(60,30)
    
            button = QPushButton('Button', self)
            button.setToolTip('This is an example button')
            button.clicked.connect(self.printValue)
            button.move(100,25)
    
            self.mySlider = QSlider(Qt.Horizontal, self)
            self.mySlider.setGeometry(30, 140, 200, 30)
            self.mySlider.setMinimum(0)
            self.mySlider.setMaximum(100)
    
            # self.textbox.setText(str(config.getminValue()))
            # textboxValue = self.textbox.text()
            # self.mySlider.setValue(int(textboxValue) + 30)
           
            self.setGeometry(50,50,320,200)
            self.setWindowTitle("Checkbox Example")
            self.show()
    
        #def changeValue(self, value):
            #print(value)
    
        def printValue(self):
            if not os.path.exists('445nm.ini'):
                textboxValue = self.textbox.text()
                print(textboxValue) 
                f = open('445nm.ini', 'w+')
                config = configparser.RawConfigParser()
                config.add_section('445nm')
                config.set('445nm', 'intensity', textboxValue)
                config.write(f)            
                if self.textbox.text() == "":
                    self.mySlider.setValue(0)            
                else:
                    self.mySlider.setValue(int(textboxValue))
            else:
                self.mySlider.setValue(config.get('445nm', 'intensity'))
        
                # if self.textbox.text() == "":
                #     self.mySlider.setValue(0)
                # else:
                #     self.mySlider.setValue(config.get('445nm', 'intensity'))
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    

    config.py

    [445nm]
    intensity = 4
    

    Thanks
    Spencer


  • Banned

    Oh the pain of using the Designer -- did you know that not using the Designer is just as easy or easier and you get actually functional code that you can easily do these things with?


  • Lifetime Qt Champion

    Hi,

    What exactly is your issue currently ?


  • Banned

    @SGaist I think he is trying to reference objects that are contained with the Designer UI he created or at least that was the gist I got from his post


  • Lifetime Qt Champion

    @Denni-0 I am not so sure as except for the "ui" word there's nothing that looks Designer related in the code @sdf1444 posted.


  • Banned

    Perhaps you are right it is just when I see UI I immediately think designer output since everything else is PY ;)


  • Banned

    @sdf1444 I do not think you understand how config files should work also you are referencing an *.ini file within your code that you do not supply making that code non-executable that said perhaps this will help you better design what you are doing

    https://martin-thoma.com/configuration-files-in-python/


Log in to reply