document.write("");

python configparser 创建ini文件,动态读取与修改配置文件,以及保存与读取字符串与QColor类型的配置

 

# 动态配置所需
from import ConfigParser
# 获取系统语系所需
import locale
# QColor 类型的传参所需
from PyQt6.QtGui import QColor 
bg_background_color = QColor(50, 50, 50, 100)
text_color = QColor(255, 255, 255, 200)
language_type = ""
# 获取系统的默认语系
def get_system_language():
    # 获取默认的区域设置
    locale_info = locale.getdefaultlocale()
    # 提取语言部分
    res = "zh_CN"
    default_language = locale_info[0]
    if default_language.startswith("en_"):
        res = "en_US"
    else:
        res = default_language
    return res


# 记录内容:
# 1. 字体颜色
# 2. 背景颜色
# 3. 国际化的语系
def create_config(filename):
    global language_type
    config = ConfigParser()
    config.add_section('Settings')
    backgroundColor = QColor(24, 24, 24, 200)
    backgroundColorStr = "{}:{}:{}:{}".format(backgroundColor.red(), backgroundColor.green(), backgroundColor.blue(), backgroundColor.alpha())
    fontColor = QColor(255, 255, 255, 250)
    fontColorStr = "{}:{}:{}:{}".format(fontColor.red(), fontColor.green(), fontColor.blue(), fontColor.alpha())
    language_type = get_system_language()
    config.set('Settings', 'text_color', fontColorStr)
    config.set('Settings', 'bg_background_color', backgroundColorStr)
    config.set('Settings', 'language_type', language_type)
    with open(filename, 'w') as configfile:
        config.write(configfile)

def read_config():
    global bg_background_color, text_color, language_type
    config = ConfigParser()
    config.read('config.ini')
    if config.has_section('Settings'):
        language_type = config.get('Settings', 'language_type')
        backgroundColor = config.get('Settings', 'bg_background_color')
        textColor = config.get('Settings', 'text_color')
        bgRed, bgGreen, bgblue, bgAlpha = backgroundColor.split(":")
        bg_background_color = QColor(int(bgRed), int(bgGreen), int(bgblue), int(bgAlpha))
        textRed, textGreen, textblue, textAlpha = textColor.split(":")
        text_color = QColor(int(textRed), int(textGreen), int(textblue), int(textAlpha))
    else:
        # 没有settings的配置,需要重新创建
        create_config('config.ini')

# 读取某个配置
def read_config_key(key):
    config = ConfigParser()
    config.read('config.ini')
    if config.has_section('Settings'):
        values = config.get('Settings', key)
        return values
    else:
        return ""

# 修改某个配置(qcolor类型)
def set_config_key_qcolor(key, qcolor_value):
    config = ConfigParser()
    config.read('config.ini')
    new_qcolor_value = "{}:{}:{}:{}".format(qcolor_value.red(), qcolor_value.green(), qcolor_value.blue(), qcolor_value.alpha())
    config.set('Settings', key, new_qcolor_value)
    with open('config.ini', 'w') as f:
        config.write(f)

# 修改某个配置(str类型)
def set_config_key_str(key, new_value):
    config = ConfigParser()
    config.read('config.ini')
    config.set('Settings', key, new_value)
    with open('config.ini', 'w') as f:
        config.write(f)
        
# 程序初始化时,检查是否有ini配置文件,如果没有,则新建,如果有,则读取
def main():
    # 读取配置
    read_config()

if __name__ == "__main__":
    main()

  

posted @ 2024-03-28 11:56  人间春风意  阅读(133)  评论(0编辑  收藏  举报