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

Support ContextManagers for GUI & container start/stop... #234

Closed
jarvisteach opened this Issue Sep 1, 2017 · 8 comments

Comments

Projects
None yet
2 participants
@jarvisteach
Owner

jarvisteach commented Sep 1, 2017

There's a lot of start/stop in appJar.

It would be nice to add in support for with, which would then add some nice indentation to help see what's going on...

Already in use for event raising & logging passing, notes here: http://effbot.org/zone/python-with-statement.htm

I'd like to be able to achieve:

with gui("my first gui") as app:
    with app.addLabelFrame("lf"):
        app.addLabel("l1", "some text")
        app.addLabel("l2", "some text")

    with app.addScrollPane("sp1"):
        app.addLabel("l3", "some text")
        app.addLabel("l4", "some text")

I'm sure it can be done easily on the main gui class, but imagine it'll take a bit more work for all the containers...

It shouldn't break backwards compatability!

@jarvisteach jarvisteach added this to the 0.09 milestone Sep 1, 2017

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

So, there are lots of interesting things available here.

This is a good read: http://preshing.com/20110920/the-python-with-statement-by-example/

We currently do the following:

def startLabelFrame(self, title, row=None, column=0, colspan=0, rowspan=0, sticky=W):
    return self.startContainer(self.C_LABELFRAME, title, row, column, colspan, rowspan, sticky)

def stopLabelFrame(self):
    if self.containerStack[-1]['type'] != self.C_LABELFRAME:
        raise Exception("Can't stop a LABELFRAME, currently in:", self.containerStack[-1]['type'])
    self.stopContainer()

And the user calls:

app.startLabelFrame("Frame 1, column=2)
app.addLabel("l1", "Some nice text")
app.stopLabelFrame()

We can replace the start/stop with a generator:

from contextlib import contextmanager
@contextmanager
def startLabelFrame(self, title, row=None, column=0, colspan=0, rowspan=0, sticky=W):
    fram = self.startContainer(self.C_LABELFRAME, title, row, column, colspan, rowspan, sticky)
    try:
        yield fram
    finally:
        self.stopContainer()

Then the user would call:

with app.startLabelFrame("Frame 1, column=2):
    app.addLabel("l1", "Some nice text")

I really like the look of this - but need to keep it backwards compatible.

It won't be necessary to have the container check, as the indentation manages the stops...

Owner

jarvisteach commented Sep 1, 2017

So, there are lots of interesting things available here.

This is a good read: http://preshing.com/20110920/the-python-with-statement-by-example/

We currently do the following:

def startLabelFrame(self, title, row=None, column=0, colspan=0, rowspan=0, sticky=W):
    return self.startContainer(self.C_LABELFRAME, title, row, column, colspan, rowspan, sticky)

def stopLabelFrame(self):
    if self.containerStack[-1]['type'] != self.C_LABELFRAME:
        raise Exception("Can't stop a LABELFRAME, currently in:", self.containerStack[-1]['type'])
    self.stopContainer()

And the user calls:

app.startLabelFrame("Frame 1, column=2)
app.addLabel("l1", "Some nice text")
app.stopLabelFrame()

We can replace the start/stop with a generator:

from contextlib import contextmanager
@contextmanager
def startLabelFrame(self, title, row=None, column=0, colspan=0, rowspan=0, sticky=W):
    fram = self.startContainer(self.C_LABELFRAME, title, row, column, colspan, rowspan, sticky)
    try:
        yield fram
    finally:
        self.stopContainer()

Then the user would call:

with app.startLabelFrame("Frame 1, column=2):
    app.addLabel("l1", "Some nice text")

I really like the look of this - but need to keep it backwards compatible.

It won't be necessary to have the container check, as the indentation manages the stops...

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

Actually, as there is no stop, do we need a start?

Could simply have a bunch of functions - named after the container: with app.labelFrame()

Owner

jarvisteach commented Sep 1, 2017

Actually, as there is no stop, do we need a start?

Could simply have a bunch of functions - named after the container: with app.labelFrame()

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

Ha - this works a treat!

Owner

jarvisteach commented Sep 1, 2017

Ha - this works a treat!

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

Had a go with this commit: fb23e90

Seems to work...

Owner

jarvisteach commented Sep 1, 2017

Had a go with this commit: fb23e90

Seems to work...

@jarvisteach jarvisteach modified the milestones: 0.08, 0.09 Sep 1, 2017

jarvisteach added a commit that referenced this issue Sep 1, 2017

Python WITH #234
It actually works!!

It now takes 2 lines to make a GUI…
@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

So, it actually worked!

It now just takes 2 lines to make a GUI:

with gui() as app:
    app.addLabel("l1", "Text")

This also support containers.

BTW - found out these are called ContextManagers

Owner

jarvisteach commented Sep 1, 2017

So, it actually worked!

It now just takes 2 lines to make a GUI:

with gui() as app:
    app.addLabel("l1", "Text")

This also support containers.

BTW - found out these are called ContextManagers

@jarvisteach jarvisteach changed the title from Incorporate python with command to Support ContextManagers for GUI & container start/stop... Sep 1, 2017

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 1, 2017

Owner

I think the openContainer functions should also be supported by ContextManagers - if the name supplied is already in use, then simply modify that container...

Owner

jarvisteach commented Sep 1, 2017

I think the openContainer functions should also be supported by ContextManagers - if the name supplied is already in use, then simply modify that container...

@mpmc

This comment has been minimized.

Show comment
Hide comment
@mpmc

mpmc Sep 1, 2017

Contributor

These are a nice addition! It's made me want to add support for with to my controller code too!

Contributor

mpmc commented Sep 1, 2017

These are a nice addition! It's made me want to add support for with to my controller code too!

jarvisteach added a commit that referenced this issue Sep 2, 2017

ContextManagers now all support opening a container #234
Also, added missing validation on startToggleFrame & startScrollPane

jarvisteach added a commit that referenced this issue Sep 2, 2017

jarvisteach added a commit that referenced this issue Sep 2, 2017

jarvisteach added a commit that referenced this issue Sep 2, 2017

@jarvisteach

This comment has been minimized.

Show comment
Hide comment
@jarvisteach

jarvisteach Sep 5, 2017

Owner

Seems there is an issue with PanedFrames - Frames within Frames, all gets a bit confusing...

Owner

jarvisteach commented Sep 5, 2017

Seems there is an issue with PanedFrames - Frames within Frames, all gets a bit confusing...

jarvisteach added a commit that referenced this issue Sep 14, 2017

Set popUp parent #249
All popUps now have an optional parent parameter

Also, added startWindow & language to GUI contextManager #234

jarvisteach added a commit that referenced this issue Sep 15, 2017

Reopen Paned Windows #234
Resolved issue with reopening paned windows #234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment