본문 바로가기
python3 selenium

python PYQT5 save, load setting Value(QLineEdit,QDateEdit,QTimeEdit,QComboBox,QSpinBox,,Q CheckBox,QradioButton)

by Pymac 2023. 12. 10.
반응형

python PYQT5 save, load setting Value(QLineEdit,QDateEdit,QTimeEdit,QComboBox,QSpinBox,QradioButton)

python PYQT5 QLineEdit,QDateEdit,QTimeEdit,QComboBox,QSpinBox,QradioButton 설장값 저장하고 불러오기

    def saveText(self):
        with open('set_values.txt', 'w') as file:
 
            # QLineEdit 데이터 저장
            id_text = self.ID_1.text()
            pw_text = self.PW_1.text()
            file.write(f'{id_text},{pw_text}\n')

            # QDateEdit 데이터 저장
            dateEdits = [self.day1]
            for dateEdit in dateEdits:
                selected_date = dateEdit.date()
                date_text = selected_date.toString("yyyy-MM-dd")
                file.write(date_text + '\n')

            #QTimeEdit 데이터 저장
            bookingtime = self.booking_time.time()
            bookingtime_text = bookingtime.toString("HH:mm:ss")
            file.write(f'{bookingtime_text}\n')

            # Combo Box1 데이터 저장
            for i in range(1, 5):
                combo_box = getattr(self, f'combo_box1_{i}')
                combo_text = combo_box.currentText()
                file.write(f'{combo_text}\n')

            #  QSpinBox 데이터 저장
            for spinBox in [self.spinBox_1, self.spinBox_2, self.spinBox_3]:
                spinBox_value = spinBox.value()
                file.write(f'{spinBox_value}\n')
 
            #Save CheckBox states
            for i in range(1, 4):
                 checkbox = getattr(self, f'checkBox_{i}')
                 checkbox_state = checkbox.isChecked()
                 file.write(f'{int(checkbox_state)}\n')
 
 
            # 라디오 버튼 상태를 문자열로 저장
            radio_button_states = [str(rb.isChecked()) for rb in [self.radioButton_1, self.radioButton_2, self.radioButton_3,        self.radioButton_4, self.radioButton_5, self.radioButton_6, self.radioButton_7, self.radioButton_8, self.radioButton_9]]
            file.write(','.join(radio_button_states))

    def loadText(self):
        try:
            with open('set_values.txt', 'r') as file:
                lines = file.readlines()

                # QLineEdit 데이터 로드
                id_text, pw_text = lines[0].strip().split(',')
                self.ID_1.setText(id_text)
                self.PW_1.setText(pw_text)

                # QDateEdit 데이터 로드
                dateEdits = [self.day1]
                line_index = 1
                for dateEdit in dateEdits:
                    if line_index < len(lines):
                        date_text = lines[line_index].strip()
                        selected_date = QDate.fromString(date_text, "yyyy-MM-dd")
                        dateEdit.setDate(selected_date)
                        line_index += 1
                       
                # Load QTimeEdit  데이터 로드
                if line_index < len(lines):
                    booking_time_text = lines[line_index].strip()
                    bookingtime = QTime.fromString(booking_time_text, "HH:mm:ss")
                    self.booking_time.setTime(bookingtime)
                    line_index += 1


                # Combo Box1 데이터 로드
                for i in range(1, 5):
                    combo_box = getattr(self, f'combo_box1_{i}')
                    if line_index < len(lines):
                        combo_text = lines[line_index].strip()
                        combo_box.setCurrentText(combo_text)
                        line_index += 1

                # QSpinBox 데이터 로드
                spinBoxes = [self.spinBox_1, self.spinBox_2, self.spinBox_3]
                for spinBox in spinBoxes:
                    if line_index < len(lines):
                        spinBox_value = int(lines[line_index].strip())
                        spinBox.setValue(spinBox_value)
                        line_index += 1
 
 
                 # Load CheckBox state
                if line_index < len(lines):
                    checkbox_state = bool(int(lines[line_index].strip()))
                    self.checkBox.setChecked(checkbox_state)
                    line_index += 1
 
                # Load CheckBox states
                for i in range(1, 4):
                    if line_index < len(lines):
                         checkbox_state = bool(int(lines[line_index].strip()))
                         checkbox = getattr(self, f'checkBox_{i}')
                         checkbox.setChecked(checkbox_state)
                         line_index += 1
 
 
                # 라디오 버튼 상태를 문자열로부터 Boolean 값으로 변환
                radio_button_states = lines[-1].strip().split(',')
                for i, rb in enumerate([self.radioButton_1, self.radioButton_2, self.radioButton_3, self.radioButton_4, self.radioButton_5, self.radioButton_6, self.radioButton_7,self.radioButton_8,self.radioButton_9 ]):
                    rb.setChecked(radio_button_states[i] == 'True')

        except FileNotFoundError:
            pass

 

반응형