Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. 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
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sdf1444
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly is your issue currently ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved