Forum Archive

Pythonista View Sidebar

DavinE

Hey Guys,

I have this code Example:

import ui
from anchors import SafeAreaView, dock, At, at, size_to_fit, flow
from objc_util import ObjCClass

def style(*views):
    for v in views:
        v.text_color = v.tint_color = v.border_color = 'yellow'
        v.border_width = 1
        v.alignment = ui.ALIGN_CENTER
    return v

class main(SafeAreaView):
    def __init__(self):
        # Bildschirmgröße
        self.WIDTH, self.HEIGHT = ui.get_screen_size()

        self.main_content = ui.View(frame=self.bounds, flex='WH', bg_color='')
        self.add_subview(self.main_content)

        self.button_area = style(ui.View(name='button_area', bg_color=''))
        dock(self.button_area).bottom(self.main_content, At.TIGHT)

        at(self.button_area).height = at(self.button_area).fit_height

        self.name = 'Barcode Scanner'
        self.background_color = 'black'
        self.present('fullscreen', hide_title_bar=True)

        self.StartPage()

    #FUNKTION StartPage
    def StartPage(self):
        user_button = size_to_fit(ui.Button(name='user_button', title='Kunden'))
        user_button.action = self.sidebar_openClose
        if user_button.image:
            user_button.width = user_button.height
        dock(user_button).bottom_center(self.button_area)

        setting_button = size_to_fit(ui.Button(name='setting_button', title='Einstellungen'))
        setting_button.action = self.sidebar_openClose
        if setting_button.image:
            setting_button.width = setting_button.height
        dock(setting_button).bottom_right(self.button_area)

        at(self.button_area).height = at(self.button_area).fit_height

    #FUNKTION sidebar_openClose
    def sidebar_openClose(self, sender):
        if sender.name != 'close_button':
            self.sidebar = style(ui.View(name='showSettingUser', width=150))
            self.add_subview(self.sidebar)
            at(self.sidebar).top = at(self.main_content).top
            at(self.sidebar).bottom = at(self.main_content).bottom
            at(self.sidebar).right = at(self.main_content).left

            close_button = size_to_fit(ui.Button(name='close_button', title='Schließen'))
            close_button.action = self.sidebar_openClose
            dock(close_button).bottom_center(self.sidebar)

        if sender.name == 'user_button':
            USERS = [
            'Name 1',
            'Name 2',
            'Name 3',
            'Name 4',
            'asjlkd kjashdgl',
            'this is a long name',
            'Hakuna Matata'
            ]

            buttons = []
            for i in USERS:
                b = size_to_fit(ui.Button(name=i, title=i))
                b.objc_instance.button().contentHorizontalAlignment=1
                buttons.append(b)

            flow(*buttons).from_left_down(self.sidebar)
        elif sender.name == 'setting_button':
            SETTINGS = [
            'Einstellung 1',
            'Einstellung 2',
            'Einstellung 3',
            'Einstellung 4'
            ]

            buttons = [
                size_to_fit(ui.Button(name=i, title=i))
                for i in SETTINGS
            ]
            flow(*buttons).from_left_down(self.sidebar)

        if sender.name != 'close_button':
            if self.main_content.x == 0:
                self.main_content.x = -self.sidebar.x
        elif sender.name == 'close_button':
            if self['showSettingUser'] != None:
                self.main_content.x = 0
                self.remove_subview(self.sidebar)

if __name__ == '__main__':
    main()

but when i run this and open and close 2 Sidebars the i get this error:

observeValueForKeyPath: <NSKeyValueObserving_ObjC: 0x283150010> <class 'AttributeError'> 'NoneType' object has no attribute 'bounds'

is in my code an issue or is this at all the right way to do this ?

Thanks
DavinE

EDIT:
another Question.. when i open the Sidebar my text are cutted... size_to_fix have no effect..
how can i fix this ?

(on my "Kunden" Button)

mikael

@DavinE, the error comes from anchors, and I have seen similar when a view does not have a superview.

In this case the problem is likely with how you ”throw away” the sidebar when you recreate it.

This tells me I should add a convenience method that you can call to remove all anchors for a view and its subviews.

But in any case, I would suggest having two different sidebars, one for customers and one for orders, not throwing them away but hiding them, and using a ui.TableView for their contents, as you would then get scrolling and editing options for free.

DavinE

@mikael said:

But in any case, I would suggest having two different sidebars, one for customers and one for orders, not throwing them away but hiding them, and using a ui.TableView for their contents, as you would then get scrolling and editing options for free.

okay i tried it with an ui.TableView

In this case the problem is likely with how you ”throw away” the sidebar when you recreate it.

That would be nice and i think a bit easier

Thanks :D

@mikael EDIT:
Is it possible That you can show me an example?
I dont get it... its always the same error or it pops both views up :(

DavinE

@mikael said:

But in any case, I would suggest having two different sidebars, one for customers and one for orders, not throwing them away but hiding them, and using a ui.TableView for their contents, as you would then get scrolling and editing options for free.

But how it works when i make more pages like..
1. Startpage
2. Add User
3. etc....

how can i close here my pages or is it then same like this:

self.root.remove_subview(view) ?

or is it here special

mikael

@DavinE, you can have two sidebars, and move the one you do not need outside the screen, or both when you want to show the main content only.

If you have several pages, you can also consider ui.NavigationView for paging.

DavinE

@mikael i found an working solution for me ;) ^^

i have an other Question... about Labels

when i use the label like:

size_to_fit(ui.Label(name='welcomeLabel', text='a very long text.........'))
size_to_fit(ui.Label(name='welcomeLabel', text='a very long **\n** text.........'))

on small Devices my text is cutted on both examples....
do you have an solution for me :D ^^

thanks and regards

JonB

It seems to me that your options are to either change the font smaller, or text wrap it so it takes up more lines, fewer columns. The textwrap module has a wrap function that is good for limiting text to some number of columns. I'm sure there is probably a iOS objc way to do the same, within the text view inside the button...

Buttons with really long text are not really the way iOS designs are supposed to be made... Usually there is a better way.

mikael

@DavinE, ui.Label has a standard feature where you set number_of_lines to 0, and it will wrap to as many lines as are needed.

It is often convenient to set the width to something suitable, then call size_to_fit to make the height fit the amount of text you have.

Many different font sizes on one UI can be ugly, but if that is not a concern, you can also have a loop where you use ui.measure_string to check if the text fits, and make the font size smaller if not.

DavinE

@JonB Thanks for your Reply but @mikael have the solution....

It is often convenient to set the width to something suitable, then call size_to_fit to make the height fit the amount of text you have.

i tried so many things but to call Size_to_fit after that...
crap---

thanks @mikael !!

DavinE

@mikael said:

@DavinE, the error comes from anchors, and I have seen similar when a view does not have a superview.

In this case the problem is likely with how you ”throw away” the sidebar when you recreate it.

This tells me I should add a convenience method that you can call to remove all anchors for a view and its subviews.

@mikael
Did you do that for anchors ?

mikael

@DavinE, do you mean an anchor removal method? I think I did not. Do you need it?

DavinE

@mikael maybe it's a better way to remove Sidebars ?
In my case i get often errors when i Rotate my iPhone (this is solved with your code in my other Post) or iPad
or an other example when i close the Sidebar and reopen it i'll get en error too..
so at the moment i don't close the View i put it away form the main view

only when it's not a lot of Work for you ;)

i Hope you understand what i want ^^
or need i do an Example ? (when i can reproduce it)