如何 save-to/read-from 归档来自 PyQt StandardButton 的回复

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

我正在尝试保存使用 PyQt 构建的应用程序的 "state";例如,如果应用程序显示信息弹出窗口并询问 "Do you want to see this message next time?";我需要将回复保存到一个文件中,以便下次用户打开该应用程序时,它会读取该文件并知道该怎么做。我写了这段代码:

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)

但我收到此错误 TypeError:write() 参数必须是 str,而不是 StandardButton。 那么如何将 reply 保存到文件中,以某种方式我以后可以从文件中读取并在 if 语句中使用。

试一试:

    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