Python Forum
Function parameters and values as string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Function parameters and values as string (/thread-28567.html)



Function parameters and values as string - infobound - Jul-24-2020

Hi all,
I am working on a project with a GUI and I am going to make the interface have multiple possible styles like "Dark Mode". So I thought the best way to store the style is in the form of a string, thinking I could pass the entire string into the configure method. Is this possible? Is there an other way to do this? Please see code below for examples.

            strStyle='activebackground="#ff8040", activeforeground="#000000", background="#c0c0c0", font="-family {Segoe UI} -size 14 -weight bold -slant roman", foreground="#000000"'

            #Passing the style string s a whole is what I would like to do, but does no work
            self.btn.configure(strStyle)
            #---------------------------------


            #so I tried to split the string into the individual options with their values, still not working
            self.btn = tk.Button(top)
            self.btn.place(x=rowCnt*97, y=0, height=42, width=97)
            self.btn.configure(text=rowTab[1])
            arrayArgAndVal=strStyle.split(",")
            for itemArgAndVal in arrayArgAndVal:
                #the first itemArgAndVal='activebackground="#ff8040"'
                self.btn.configure(itemArgAndVal)
            #---------------------------------


            #then I tried splitting the option from the value, still not working
            self.btn = tk.Button(top)
            self.btn.place(x=rowCnt*97, y=0, height=42, width=97)
            self.btn.configure(text=rowTab[1])
            arrayArgAndVal=strStyle.split(",")
            for itemArgAndVal in arrayArgAndVal:
                #the first itemArgAndVal='activebackground="#ff8040"'
                itemArgAndVal=itemArgAndVal.split("=")
                self.btn.configure(itemArgAndVal[0]=itemArgAndVal[1])
            #---------------------------------



RE: Function parameters and values as string - scidam - Jul-24-2020

I would recommend to organize your configration settings as a Python dictionary, e.g. = }{

default_style = {
'btn': {'activebackground': "#ff8040", 
        'activeforeground': "#000000"'
        },
'text': {'font': 'xxx'},

'other element': {a dict}
}
dark_theme can be based on default style;

Finally, you can traverse the dictionary and apply configurations to each eleement, e.g do something like this:

def apply_conf(self, conf):
    for k, v in conf.items():
        if hasattr(self, k):
           getattr(self, k).configure(**v)