0

I'm using python 3.8.10 in the spyder IDE to create a GUI test program with a settings menu. the settings menu currently has 4 check boxes that are checked or unchecked when the settings menu is loaded by reading from a text file that just contains 4 numbers either 1 or 0 to denote checked or unchecked. i have an apply button and a cancel button. apply should save the state of each button and write it to the file and then close the settings menu. The cancel button shouldn't save anything and just close the settings menu. the problem is that the cancel button is saving whatever changes i make to the check boxes too.

def quitfunc():
    print("quit")
    app.closeAllWindows()
    sys.exit()
    
def tosettings():
    print('settings')
    mainwindow = settings()
    widget.addWidget(mainwindow)
    widget.setCurrentIndex(widget.currentIndex()+1)
    
def closesettings():
    print('close settings')
    mainwindow = Loadtest()
    widget.addWidget(mainwindow)
    widget.setCurrentIndex(widget.currentIndex()-1)

class settings(QDialog):
    def __init__(self):
        super(settings,self).__init__()
        loadUi("settingspage.ui",self)
        self.cancel.clicked.connect(lambda: closesettings())
        self.apply.clicked.connect(self.applysettings)
        pixmap1 = QPixmap('picture1.png')
        self.label.setPixmap(pixmap1)
        with open('settings.txt') as settingsfile:
            print('openfile')
            read = None
            read = settingsfile.read()
            print(read)
            self.checkBox.setCheckState(int(read[0]))
            print(self.checkBox.checkState())
            self.checkBox2.setCheckState(int(read[1]))
            self.checkBox3.setCheckState(int(read[2]))
            self.checkBox4.setCheckState(int(read[3]))
    
    def applysettings(self):
        with open('settings.txt','w') as settingsfile:
            print('savesettings')
            towrite = ''
            towrite=str(self.checkBox.checkState())
            towrite+=str(self.checkBox2.checkState())
            towrite+=str(self.checkBox3.checkState())
            towrite+=str(self.checkBox4.checkState())
            settingsfile.write(towrite)
            closesettings()
        

class Loadtest(QDialog):
    def __init__(self):
        super(Loadtest,self).__init__()
        loadUi("loadtest.ui",self)
        self.load.clicked.connect(self.loadfunction)
        self.settings.clicked.connect(lambda: tosettings())
        self.quit.clicked.connect(quitfunc)
        pixmap1 = QPixmap('picture1.png')
        self.label.setPixmap(pixmap1)
        
    def loadfunction(self):
        print('load')
        item = self.listWidget.currentItem()
        print(item.text())

app=QApplication(sys.argv)

mainwindow=Loadtest()
widget=QtWidgets.QStackedWidget()
widget.setWindowTitle("Test Loader.exe")
widget.addWidget(mainwindow)
widget.setFixedWidth(800)
widget.setFixedHeight(1000)
widget.show()
app.exec_()

ive tried opening the settings file between actions and its always correct. settings.txt contains "1011" 1st,3rd and 4th boxes are checked i tick the 2nd box click cancel and settings menu closes open settings.txt and it shows "1011" as it should as i didnt click apply to save settings open settings menu from GUI again and all 4 boxes are checked. the program is just ignoring whats in the settings file for some reason?

  • The program is correct, the logic is faulty. Every time `tosettings()` is called it adds a *new* `settings` instance, but `widget.setCurrentIndex(widget.currentIndex()+1)` actually goes to the page that was previously created. You are probably following a YouTube tutorial that is known to provide ***a lot*** of terrible suggestions and bad practices, so I strongly suggest to completely ignore it. QStackedWidget works almost like a tab widget, but without a tab bar: it uses *pages* that can be switched, so you are expected to *reuse* pages, not continuously creating them. – musicamante Jul 20 '23 at 14:15

0 Answers0