Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法从ini文件中读取布尔值_Python_Python 3.x_Pyqt_Pyqt5_Qsettings - Fatal编程技术网

Python 无法从ini文件中读取布尔值

Python 无法从ini文件中读取布尔值,python,python-3.x,pyqt,pyqt5,qsettings,Python,Python 3.x,Pyqt,Pyqt5,Qsettings,我无法从PyQt5app中的ini文件加载布尔值 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5 import QtCore from PyQt5.QtCore import QSettings, QVariant from PyQt5.QtWidgets import ( QApplication, QCheckBox, QDialog, QGridLayout, QLabel, QLayout

我无法从
PyQt5
app中的
ini
文件加载布尔值

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtCore
from PyQt5.QtCore import QSettings, QVariant
from PyQt5.QtWidgets import (
    QApplication, QCheckBox, QDialog, QGridLayout,
    QLabel, QLayout, QPushButton
)


class Settings(QDialog):
    "settings GUI"

    _settings = None

    def __init__(self):
        super(Settings, self).__init__()
        self._ui()

    def _ui(self):
        self._chk_test = QCheckBox()
        self._chk_test.setText("test checkbox")

        self._settings = QSettings("settings.ini", QSettings.IniFormat)
        self._settings.setFallbacksEnabled(False)

        # load configuration
        self._chk_test.setChecked(
            self._bool(self._settings.value("test_value", True)))

        # save settings
        btn_save = QPushButton("save")
        btn_save.clicked.connect(self._save_settings)

        # setting layouts
        grid_layout = QGridLayout()
        grid_layout.addWidget(self._chk_test, 0, 0, 1, 1)
        grid_layout.addWidget(btn_save, 0, 1, 1, 1)
        grid_layout.setSizeConstraint(QLayout.SetFixedSize)
        grid_layout.setHorizontalSpacing(100)
        grid_layout.setVerticalSpacing(5)

        self.setWindowTitle("Boolean")
        self.setLayout(grid_layout)
        self.show()

    def _save_settings(self):
        self._settings.setValue("test_value", self._chk_test.isChecked())
        self.close()

    def _bool(self, str):
        if str == "true":
            return True
        else:
            return False


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = Settings()
    sys.exit(app.exec_())
self._settings.value("test_value", True).toBool()
当我尝试使用

self._settings.value("test_value", True).toBool()

self._settings.value("test_value", True).toBool()
我得到了一个属性错误:

self._settings.value("test_value", True).toBool()
'str' / 'QVariant' object has no attribute 'toBool()'. 

我已经编写了自定义的
\u bool
方法,但我想知道是否有更好的方法来解决它。

假设
self.\u settings.value(“test\u value”,True)
返回一个字符串,其值为
“True”
,您可以这样做

self._settings.value("test_value", True).toBool()
self._chk_test.setChecked(
            self._settings.value("test_value", True) == "true")
但是,如果您需要多次执行此操作,我将保留自定义方法(但要按照@furas的建议使其更加紧凑):

self._settings.value("test_value", True).toBool()

他们添加了第三个参数
type=
,以设置结果的类型。因此,您可以使用
type=bool
获取Python
True/False
而不是字符串
“True”/“False”

self._settings.value("test_value", True).toBool()
结果

self._settings.value("test_value", True).toBool()
a: <class 'bool'> True
b: <class 'str'> true
a:对
b:是的

可在谷歌上找到:

self._settings.value("test_value", True).toBool()
  • 将可选的type关键字参数添加到QSettings.value()以允许 要指定的返回值的类型

更短:
returnstr==“true”
很好的技巧,谢谢!我没有在Qt文档中看到,我认为它不存在,但是您可以在中找到它。谢谢您的回答。这正是我想要做的。它返回一个字符串,它的值可以是“真”或“假”,我想让它成为布尔值。上面给出了我想要的答案。。