Echeban
Echeban

Reputation: 204

How to save-to/read-from file the reply from a PyQt StandardButton

I am trying to save the "state" of an App built with PyQt; for example, if the App shows an info popup and asks "Do you want to see this message next time?"; I need to save the reply to a file, so next time the user opens the app, it reads the file and knows what to do. I wrote this code:

reply = QMessageBox.question(self, 'Warning', text, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
print (reply) # No:65536, Yes=16384 
with open('state.txt', 'w') as f:
    f.write(reply)

but I get this error TypeError: write() argument must be str, not StandardButton. So how do I save the reply to file, in some way that I can later read from file and use in an if statement.

Upvotes: 0

Views: 205

Answers (1)

S. Nick
S. Nick

Reputation: 13641

Try it:

    reply = QMessageBox.question(
        self, 
        'Warning', 
        'text', 
        QMessageBox.Yes | QMessageBox.No, 
        QMessageBox.Yes
    )

    print (reply, type(reply))                   # No:65536, Yes=16384 

    if reply == QMessageBox.Yes:
        value = "Yes"                            # or  value = "16384"
    else:
        value = "No"                             # or  value = "65536"

    with open('state.txt', 'w') as f:
        f.write(value)                           # !!!     value

Upvotes: 1

Related Questions