Python Forum
Function parameters and values as string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function parameters and values as string
#1
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]) #---------------------------------
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to compare string values in an if statement israelsattleen 1 1,518 Jul-08-2023, 03:49 PM
Last Post: deanhystad
Question How to compare two parameters in a function that has *args? Milan 4 3,797 Mar-26-2023, 07:43 PM
Last Post: Milan
  Adding values with reduce() function from the list of tuples kinimod 10 7,423 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Getting rid of old string values Pedroski55 3 2,652 Oct-11-2022, 10:56 PM
Last Post: Pedroski55
  mutable values to string items? fozz 15 6,872 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  function accepts infinite parameters and returns a graph with those values edencthompson 0 1,738 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Convert a string to a function mikepy 8 6,934 May-13-2022, 07:28 PM
Last Post: mikepy
  Function - Return multiple values tester_V 10 7,831 Jun-02-2021, 05:34 AM
Last Post: tester_V
  How can I write a function with three parameters? MehmetAliKarabulut 1 3,538 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  Parameters aren't seen inside function Sancho_Pansa 8 5,448 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.