Adhere to OOP concepts
See original GitHub issueIf we stick to OOP concepts, future development will be a lot easier. I suggest creating a widget class which instantiates an underlying tkinter
widget of sorts when called:
import tkinter as tk
class _Widget:
"""An abstract `tk` widget."""
def __init__(self, widgettype, **kwargs):
"""Instantiate `widgettype(**kwargs`)."""
self._widget = widgettype(**kwargs)
def enable(self):
"""Enables the widget."""
self._widget.configure(state="normal")
def disable(self):
"""Disables the widget."""
self._widget.configure(state="disabled")
We can then simply create _Widget
subclasses, without the cluttered default Tcl options:
import tkinter as tk
from _widget import _Widget
class Text(_Widget):
"""A Text widget displays text."""
def __init__(self, text="", size=12, color="black", font="helvetica", grid=None, align=None):
super().__init__(
widgettype=tk.Label,
text=text,
size=size,
foreground=color,
font=font
)
# Do stuff with `grid` and `align`...
This has a couple of advantages:
-
Methods that can be applied to all widgets (like
enable
/disable
for switching state,repeat
to repeat a callable every x milliseconds and display options for hiding, showing, aligning widgets) can be inherited from_Widget
. -
Reusing methods means having to write only one docstring (e.g.
_Widget.enable
: ‘enables the widget’ instead ofText.enable
: ‘enables the text widget’;Button.enable
: ‘enables the button widget’; etc.) -
Validating argument types / values can be done within
_Widget.__init__
(or using helper functions) instead of doing this individually for each widget type.
What do you think?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
I’ve already implemented #38 on the version 0.4 branch as per @bennuttall 's suggestion, which puts the TK object inside the guizero object, so that any of the tk functionality people might want to access can be easily got at via
Object.tk.methodname
. However it might be useful to have the functions that all widgets will need to use (e.g. after, enable, disable) written in one place and then inherited. I was actually thinking about this yesterday. Would combining the two be possible?Pulled #57 and ready for testing across all widgets