从配置文件中读取 python ttk 样式

Reading python ttk Styles from configuration file

使用 python ttk 和 ConfigParser 导致我遇到以下问题。

我想使用配置文件使样式在每个用户的使用过程中都能适应 - 不仅是那些有权访问源的用户。

在我的 python 脚本 (Python 2.7) 中使用来自硬编码部分的 ttk 样式非常有效

def LoadStyles(self):
  s=ttk.Style()

  if not os.path.exists("Styles.cfg"):
     s.configure('TFrame', background='white')
     s.configure('Dark.TFrame', background='#292929')
  else:
     config=ConfigParser.RawConfigParser()
     config.read("Styles.cfg")

     for section in config.sections():
        for (key, val) in config.items(section):
           try:
              s.configure(section, key="%s"%val)
           except:
              print("Error Parsing Styles Config File:  [%s].%s = %s"%(section, key, val))

使用配置文件(下面的内容)会导致所有框架的白色背景,以及像

这样声明的框架
self.loginFrame=ttk.Frame(self, style='Dark.TFrame')

编辑: 它们不是白色但未填充(默认填充)。

样式是在加载小部件之前通过硬编码或配置文件两种方式完成的。

我只是不明白我被困在这里的地方,手册和 SO 搜索没有给我关于那个的任何答案...

[TFrame]
background = "white"
[Dark.TFrame]
background = "#292929"

非常感谢任何帮助。

终于找到解决办法了:

问题是 "key" 是作为关键字写入样式的。 例如 {'key': '#292929'} 可以使用

读取此数据
print(s.configure(section))

之后
s.configure(section, key="%s"%val)

关键字解包是线索: (非常感谢 SO

for section in config.sections():
    for (key, val) in config.items(section):
        try:
            test={ "%s" % key : "%s" % val }
            s.configure(section, **test)
        except:
            print("Error Parsing Styles Config File:")
            print("   [%s].%s = %s"%(section, key, val))

现在也可以使用从配置文件中读取的样式。