question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adhere to OOP concepts

See original GitHub issue

If 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 of Text.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:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lawsiecommented, Nov 15, 2017

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?

0reactions
lawsiecommented, Dec 5, 2017

Pulled #57 and ready for testing across all widgets

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object-Oriented Programming Principles in Java
Object-oriented programming is a programming paradigm where everything is represented as an object. Objects pass messages to each other.
Read more >
Why Object-Oriented Programming Matters - Apollo Technical
The idea behind OOP was to put mini-computers in software that can communicate via message passing rather than through direct data sharing. This ......
Read more >
How to craft better code using OOP Principles
We have covered some basic principles in object-oriented programming. Abstraction, Composition, Encapsulation, and Polymorphism are important concepts to ...
Read more >
The Four Pillars of Object Oriented Programming
The Four Principles of Object-Oriented-Programming (OOP): · Encapsulation · Abstraction · Inheritance · Polymorphism · Comments.
Read more >
Principles of Object Oriented Programming - Level Up Coding
To keep object oriented systems as clean and error proof as possible we must adhere to the 4 fundamental principles of OOP: Encapsulation,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found