0

should you create a factory method to create tkinter widgets or is it considered best practice not to?

for example a program with say 10 entry boxes, 5 buttons, and 16 labels (just random numbers) would have 31 occurences of pretty much the same code most commonly like this:

self.a_widget = ttk.widget_type(self,text="hello world!")
self.a_widget.config(foreground="white",background="black")
self.a_widget.config(font=("Calibri",15,"bold"))
self.a_widget.pack()

so creating a factory method for this could be fairly straight forward and would certainly prevent having nearly the same code over and over.

would doing so be beneficial for future use or is something like this discouraged?

2 Answers 2

2

Go ahead and do it. Abstracting out repetitive behavior follows the DRY principle, and there's nothing special about Tkinter that disqualifies the practice from being used here.

Sign up to request clarification or add additional context in comments.

2 Comments

tkinter was just an example
Following the DRY principle is also good even when not using Tkinter.
1

If using a factory makes the code less complex and easier to understand, then yes, use a factory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.