Search code examples
pythonkivykivy-language

Kivy's ScreenManager and Popups don't want to work together


as stated in the title - I'm stuck. I've been playing with the code around and everything works as long as I keep ScreenManager and Popup separate. Once combined - they refuse to cooperate. Anyway, here is the simple app that shows the problem I'm having.

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager

class First(GridLayout,Screen):
    def show_popup(self):
        Popp().open()
    pass

class Second(Screen):
    pass

class Popp(Popup):
    pass

class ScreenManagement(ScreenManager):
    pass

app = Builder.load_file("main.kv")

class MainApp(App):
    def build(self):
        return app


if __name__ == "__main__":
    MainApp().run()

And main.kv file

ScreenManagement:
    First:
    Second:

<First>:
    name:"First"
    rows: 2
    Button:
        text: "FirstButton"
        on_release: app.root.current = "Second"
    Button:
        text: "Show popup"
        on_release: root.show_popup()

<Second>:
    name:"Second"
    Button:
        text: "BUTTON"
        on_release: app.root.current = "First"

<Popp>:
    title: "testing"
    text: "Hello world"
    size_hint: None,None
    size: 400,400
    auto_dismiss: False
    Button:
        text: "Okay"
        on_press: root.dismiss()

App starts, first and second screen are working but when trying to get popup up I end up with:

kivy.uix.popup.PopupException: Popup can have only one widget as content

Somehow Screen is seen as a widget inside of Popp? Or am I terribly misinterpreting kivy docs?


Solution

  • It's a bug with loading kv file, it should throw an exception in this case.

    What you are doing in the code is loading the kv file twice, what causes some weird behavior. Just delete the Builder.load_file(..) and it will work. The file is going to be loaded automatically.

    Also, never do double subclassing of widgets like class First(GridLayout, Screen) as it might lead to some problems. Instead, create a grid layout inside the screen.