Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider an alternative to extending tk classes #38

Closed
bennuttall opened this issue Aug 21, 2017 · 9 comments
Closed

Consider an alternative to extending tk classes #38

bennuttall opened this issue Aug 21, 2017 · 9 comments

Comments

@bennuttall
Copy link
Contributor

A suggestion: rather than extend tk classes, would you consider creating new classes where the tk object is exposed as a property, so that only the guizero goodness is exposed to most users, but those who wish to (forgive me) tinker below the surface can still get to the nasty tk stuff?

The tk stuff is messy and most of it isn't useful. Using tab completion to find property and methods you're looking for is really difficult:

screenshot from 2017-08-21 12-18-03

For example:

class Slider:
    def __init__(self, master, start=0, end=100, horizontal=True, command=None, grid=None, align=None):
        orient = HORIZONTAL if horizontal else VERTICAL
        self.tk = Scale(master, from_=start, to=end, orient=orient, command=command)
        utils.auto_pack(self, master, grid, align)

    @property
    def value(self):
        return (self.tk.get())

    @value.setter
    def value(self, value):
            self.tk.set(value)

    def add_command(self, command):
        self.tk.config(command=command)

So instead of inheriting Scale and running super()__init__() you create a tk Scale object belonging to the guizero Slider object. You would provide guizero alternatives to anything useful within the tk object, such as value and add_command as shown. Any methods which need access to the tk object just refer to self.tk instead of self.

The result:

screenshot from 2017-08-21 12-43-57

Somewhat related to @martinohanlon's #25

@lawsie
Copy link
Owner

lawsie commented Aug 23, 2017

That seems nice. Need to find some time to work on this - haven't had much lately!

@lawsie
Copy link
Owner

lawsie commented Oct 3, 2017

Nice. I implemented this for Slider in a test version and it seems pretty cool. I don't have an IDE that offers the auto complete though so can't test that.

@bennuttall
Copy link
Contributor Author

pip install ipython

It's just like the Python shell but better (no harder to use) 👍

@lawsie
Copy link
Owner

lawsie commented Oct 6, 2017

I think this approach runs into problems if you try to apply it to the App class. Because the other objects have to reference App, if you have an embedded Tk object within an App object, when another object references the App object as its master, it lacks the necessary properties of the Tk object to be able to act as the master. At least, I think that's what is happening. So, potentially I could apply this principle to all of the classes barring App (and possibly Box which is also a container)?

@bennuttall
Copy link
Contributor Author

Yeah if you can't make it work for App or Box, then you could always leave them as they are.

@DavidWhaleMEF
Copy link

The app could have a tk property, and the child classes could access App.tk - so any child accesses the tk via it's parent (container) tk attribute. Just saying, because I might or might not have solved a similar problem myself recently :)

Tip: Get your head around the 'is-a' vs 'has-a' relationships, to get stronger architecture.

@lawsie
Copy link
Owner

lawsie commented Nov 3, 2017

I believe I have done this with commit 757beea

@lawsie lawsie closed this as completed Nov 3, 2017
@bennuttall
Copy link
Contributor Author

Neat! So we're just waiting for the 0.4 release? I'll test it when I get chance.

@lawsie
Copy link
Owner

lawsie commented Nov 6, 2017

Well, I have various other things that are unfinished and might not work - in particular the after() method, and some other extras I want to add. However I think I've successfully converted all of the classes to have an internal tk object!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants