Forum Archive

Exception when opening ui.View using popover style

skrohmer

Hi there,

Please have a look at this app:

import ui
from scene import *

class MyScene(Scene):
    def setup(self):
        self.menu = ui.View(frame=(0, 0, 200, 200))

    def touch_began(self, touch):
        self.menu.present('popover', popover_location=touch.location)


run(MyScene())

If I touch any point on the screen the view opens as popup window. When I then touch any point outside the view it closes. So far this should be the normal function. But when I touch the screen again I get an exception:

Value error: View is already being presented or animation in progress

What's wrong? It also happens after waiting some seconds, so the view seems to be really closed.

Stefan

cvp

I think (but not sure) that your popover view is not really closed, but only hidden, thus you can't present it more than once.

skrohmer

I'm not sure, both self.menu.hidden and self.menu.on_screen are always False. They never get True, even if the view is displayed. I have hoped that at least self.menu.on_screen will be True as soon as I see the view.

cvp

I understand but the same exception occurs when you try to present two times the same view, even if not popover. You need to close it before the 2nd present.

cvp

Try this, only to see the result

import ui
from scene import *
class MyScene(Scene):
    def setup(self):
        self.menu = ui.View(frame=(0, 0, 200, 200))
        self.menu.name = 'popup'
    def touch_began(self, touch):
        self.menu.present('popover',popover_location=touch.location)
        ui.delay(self.popup_close,2)
    def popup_close(self):
        self.menu.close()
run(MyScene())
skrohmer

Thanks, good hint, at least this works! The problem which I see at the moment is when I "hide" the popup as described -> close() does not seem to have an effect anymore. I'm still wondering which state the hidden popup has. The class variables don't change, independent if it is not visible, presented or "hidden" by me:

obj.__dict__ = {}
obj.alpha = 1.0
obj.autoresizing = 
obj.background_color = (0.0, 0.0, 0.0, 0.0)
obj.bg_color = (0.0, 0.0, 0.0, 0.0)
obj.border_color = (0.0, 0.0, 0.0, 1.0)
obj.border_width = 0.0
obj.bounds = (0.00, 0.00, 200.00, 200.00)
obj.center = (100.00, 100.00)
obj.content_mode = 0
obj.corner_radius = 0.0
obj.flex = 
obj.frame = (0.00, 0.00, 200.00, 200.00)
obj.height = 200.0
obj.hidden = False
obj.left_button_items = None
obj.multitouch_enabled = False
obj.name = popup
obj.navigation_view = None
obj.on_screen = False
obj.right_button_items = None
obj.subviews = ()
obj.superview = None
obj.tint_color = (0.0, 0.47843137383461, 1.0, 1.0)
obj.touch_enabled = True
obj.width = 200.0
obj.x = 0.0
obj.y = 0.0
cvp

Yes, I understand. I had checked with

print(dir(self.menu))

If there was not an unknown (by me) attribute.

cvp

You can also try this: when you tap, the menu appears or disappears but its position is not ok

import ui
from scene import *
class MyScene(Scene):
    def setup(self):
        pass
        #self.menu = ui.View(frame=(0, 0, 200, 200))
    def touch_began(self, touch):
        try:
            self.menu = ui.View(frame=(0, 0, 200, 200))
            self.menu.present('popover',popover_location=touch.location)
        except: 
            self.menu.delete()          
        #ui.delay(self.popup_close,2)
    #def popup_close(self):
        #self.menu.close()
run(MyScene())
skrohmer

Yes, seems to be better. I will check again which is the best solution for me. I'm still struggling with this popup. Maybe someone else has a hint, too. Maybe @omz can tell us why the popup does not disappear completely when I click beside it.

BTW: The position seems to be strange because the coordinate systems are different. The popup placed at position 0,0 appears in the top left corner, but the point 0,0 of the touch coordinates is in the bottom left corner. The Y axes are leading in different directions.

cvp

Strange y axe 🤕

skrohmer

This seems to be the shortest solution for now:

import ui
from scene import *

class MyScene(Scene):
    def setup(self):
        pass

    def get_viewpos_from_touch(self, touch):
        xt, yt = touch.location
        xw, yw = ui.get_window_size()
        return xt, yw - yt

    def touch_began(self, touch):
        menu = ui.View(frame=(0, 0, 200, 200))
        menu.present('popover', popover_location=self.get_viewpos_from_touch(touch))


run(MyScene())

I can live with that at least at the moment. I hope that "hiding" the popup as I already explained does not result in memory leaks. ;-)

However, I'm still interested in any information which explains the strange behavior mentioned above: Why does the popup not close completely?

cvp

I think that at each touch_began run, you create a new instance of ui.View, without deleting the previous one! Not vital but not the deal, I suppose.