无法从 ini 文件中读取布尔值
Cannot read boolean from ini file
我在 PyQt5
应用中从 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()
或
QVariant(self._settings.value("test_value", True)).toBool()
我收到 AttributeError:
'str' / 'QVariant' object has no attribute 'toBool()'.
我已经编写了自定义 _bool
方法,但我想知道是否有更好的方法来解决它。
假设self._settings.value("test_value", True)
returns一个值为"true"
的字符串,你可以
self._chk_test.setChecked(
self._settings.value("test_value", True) == "true")
但是,如果您需要多次执行此操作,我会保留自定义方法(但按照@furas 的建议使其更紧凑):
def _bool(self, str):
return str == "true"
他们添加了第三个参数 type=
来设置结果的类型。所以你可以使用 type=bool
并得到 Python True/False
而不是字符串 "true"/"false"
a = self._settings.value("test_value", True, type=bool)
print('a:', type(a), a)
b = self._settings.value("test_value", True)
print('b:', type(b), b)
结果
a: <class 'bool'> True
b: <class 'str'> true
在 Google 中找到:https://riverbankcomputing.com/pipermail/pyqt/2011-January/029032.html
- Added the optional type keyword argument to QSettings.value() to allow
the type of the returned value to be specified.
我在 PyQt5
应用中从 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()
或
QVariant(self._settings.value("test_value", True)).toBool()
我收到 AttributeError:
'str' / 'QVariant' object has no attribute 'toBool()'.
我已经编写了自定义 _bool
方法,但我想知道是否有更好的方法来解决它。
假设self._settings.value("test_value", True)
returns一个值为"true"
的字符串,你可以
self._chk_test.setChecked(
self._settings.value("test_value", True) == "true")
但是,如果您需要多次执行此操作,我会保留自定义方法(但按照@furas 的建议使其更紧凑):
def _bool(self, str):
return str == "true"
他们添加了第三个参数 type=
来设置结果的类型。所以你可以使用 type=bool
并得到 Python True/False
而不是字符串 "true"/"false"
a = self._settings.value("test_value", True, type=bool)
print('a:', type(a), a)
b = self._settings.value("test_value", True)
print('b:', type(b), b)
结果
a: <class 'bool'> True
b: <class 'str'> true
在 Google 中找到:https://riverbankcomputing.com/pipermail/pyqt/2011-January/029032.html
- Added the optional type keyword argument to QSettings.value() to allow the type of the returned value to be specified.