简体   繁体   English

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

[英]Reading python ttk Styles from configuration file

Using python ttk and the ConfigParser leads me to the following issue. 使用python ttk和ConfigParser会导致以下问题。

I want to use a Configuration File to make Styles adaptable during usage for every user - not only the ones with access to the sources. 我想使用配置文件使样式在使用期间适合每个用户-不仅限于有权访问源的样式。

Using the ttk-styles inside my python script (Python 2.7) from hardcoded sections work like a charm 在我的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))

Using the Configuration File (contents below) results in white backgrounds for all Frames, also the ones that are declared like 使用配置文件(下面的内容)会为所有框架生成白色背景,也将声明为

self.loginFrame=ttk.Frame(self, style='Dark.TFrame')

EDIT: they are not white but non-filled (default fill) . 编辑:它们不是白色,而是未填充(默认填充)。

The styling is both ways done before the widgets are loaded, either hardcoded or by config file. 在加载小部件之前,两种方式都是通过硬编码或通过配置文件完成的。

I just don't get where i am stuck in here, manuals and SO Search just did not give me any answers on that one... 我只是不明白我在这里所处的位置,手册和SO Search并没有给我关于那一个的任何答案。

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

Any help is really appreciated. 任何帮助都非常感谢。

Finally i got the solution: 终于我得到了解决方案:

The issue was that "key" was written as keyword into the style. 问题是“键”被作为关键字写到样式中。 eg {'key': '#292929'} This data could be read using 例如{'key': '#292929'}此数据可以使用

print(s.configure(section))

after the 之后

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

Keyword unpacking was the clue: (Thanks a lot to SO ) 关键字解包是一个线索:(非常感谢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))

Now also the Styles read from the Configuration File can be used. 现在,也可以使用从配置文件读取的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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