2

I want to write a function to set an attribute of a class equal to an inputted value. The problem is that I don't want to know the name of the attribute.

The reason I want to do this is because I want to make a factory class for tkinter widgets that will take in the desired default settings. The factory class will have a method that instantiates a tkinter.Button with the default settings unless overridden upon calling the method.

I'd rather not hard code the settings if possible as I'd like to use this for multiple projects so having to make copies and edit them for each project isn't ideal.

This is the pseudocode for what I'm trying to do:

from tkinter import Button

class Factory:
  def __init__(self, **kwargs):
      for attr_name in list(kwargs.keys()):
        self.attr_name=kwargs[attr_name]

  def makeButtonWithText(self, text=None):
    if text is None:
      if self.text exists:
        Button(TK(), text)

      else:
        Button(Tk())

    else:
        Button(Tk(), text)
SMC-242
  • 19
  • 1
  • 1
    Are you aware this will create an additional root window each time you call the factory? – Bryan Oakley Nov 19 '19 at 18:26
  • Yes. It's just an example of the bare minimum of the factory rather than a fully fleshed out version. The real version that I'm making will work for all widgets – SMC-242 Nov 20 '19 at 08:47

0 Answers0