简体   繁体   中英

Booleans etc with Kivy ConfigParser

I've created a settings.ini and am trying to get my head around the Kivy Config Parser. I'm having to enclose all my 'get' statements in str() for most of it to work, so I wonder if I'm misunderstanding something fundamental. Also, I cannot get booleans to report correctly, so:

settings.ini

[settings]
option_enabled = False

test.py

from kivy.config import ConfigParser
config = ConfigParser()
config.read('settings.ini')

print(config.get('settings', 'option_enabled'))

returns 'False'

if config.get('settings', 'option_enabled'):
    print "true"

returns 'true'

Can anyone tell me where I'm going wrong?

[edited, as OP changed the question by correcting a typo]

I am not familiar with kivy, and cannot test, but here is my take on it:

I believe that config.get('settings', 'option_enabled') returns a string value, so when you enclose this with a print() statement, it will print the string value 'False'

when you use the same in the if statement, the string value in the if statement will return 'True' invoking your print "True"

can you try that and let me know that is the answer ?

As "Edwin van Mierlo" answered, you are getting a string out of config.get...

My suggestion is to use config.getint

ini file

[settings]
option_enabled = 0

code:

if config.getint('settings', 'option_enabled'): #notice the getint instead of get
    print "true"

you are facing this problem because python's standard ini parser writes and expects boolean values as True/False, while kivy's ConfigParser expects (and writes) 0/1. That's the reason you are probably seeing panels created with off switchs when indeed they are defined as True in the ini. However, if you save a boolean from kivy's settings panel, you'll see that it will get saved as 1 (contrary to True). The good part is that python's standard ini parser also understands 0/1. The bad part is that the default values written by python's standard ini manager are saved as True/False, and when picked up by kivy (since those values are not recognized) they will default to 0 (false).

According to kivy docs you can use a custom form of a BooleanSettings and tell it to read and write True/False values (instead of 0/1), but I haven't found yet where exactly is that those settings should be instantiated (since everything is done automatically by ConfigParser).

Not a full answer, but hope it helps anyways.

I've had the same issue today. Finally solved by overriding the SettingBoolean.

from kivy.uix.settings import Settings, SettingBoolean

class MySettingBoolean(SettingBoolean):
    values = ListProperty(['False', 'True'])

s = Settings()
s.register_type('bool', MySettingBoolean)

This keeps the switches in the Settings panel and the True / False values in the ini file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM