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?
Spencer Du
spencerdu at hotmail.co.uk
Tue Oct 1 11:51:35 EDT 2019
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
ui.py
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
More information about the Python-list
mailing list