I am doing a small GUI program for a practice exercise and in order to save myself some typing I took it upon myself to subclass some Tkinter widgets so I can pass the grid placement in the instantiation. One of my subclasses looks like this:
import tkinter as tk class EntryBox(tk.Entry): def __init__(self, master=None, width=1, relief=None, row=None, column=None, sticky=None, padx=None, pady=None, **kwargs): super().__init__(master=master, width=width, relief=relief, **kwargs) self.grid(row=row, column=column, sticky=sticky, padx=padx, pady=pady)
And one of my instances of this widget looks like this:
window = tk.Tk() title_entry = EntryBox(window, 10, 'sunken', 0, 1) window.mainloop()
My initial thinking for the subclass is that if I am creating these entry widgets, they will clearly be visible on the screen (similar for some Labels, text areas, buttons, etc...). So this would save many lines of code and typing.
Is it appropriate to subclass these widgets to save typing, or does this typically cause more confusion when another programmer reads it? Also, will this be considered more error-prone? (Perhaps it would still be best to pass named parameter in my instantiation?)
I'd like to get some comments on when it's good practice / appropriate to subclass and when it causes more confusion.
I wanted to add that I decided to pass default values in the init subclass method so that if someone wanted to call the grid methods in the traditional way they still could in the code.