Search code examples
pythonkivypopupkivy-language

Popup Defocussing without a popup showing - Kivy - Python


In the process of building an application in Kivy, I stumbled across this behaviour of my popup when the label text was too large for the size of the popup.

enter image description here

I have decided that i prefer the look of the error message showing in this way and I was wondering if there was a way to get this to happen but without the popup box showing underneath the error message?

I tried setting the size_hint vales to zero but that just landed me with this

enter image description here

Is there a way to achieve this defocussing of the main window without a popup box showing?

Thanks!

EDIT

I have now included a minimal working example, apologies for having not done so previously.

As a reminder, i'm looking for a way to get the error message to appear as an overlay to the whole app window, but without the popup window showing too.

Thanks!

main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup


class Example(App):

    def build(self):
        return ExampleWindow()


class ExampleWindow(Button):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.popup = MessageBox()


class MessageBox(Popup):
    pass


if __name__ == '__main__':
    Example().run()

Example.kv

<ExampleWindow>:
    text: "Open Message"
    on_release: root.popup.open()

<MessageBox>:
    size_hint: 0.3, 0.3
    Label:
        font_size: 36
        text: "MESSAGE TEXT IS HERE!"

Solution

  • you can not customize the Popup widget but yiu can create your own custom one by using the prenet widget of the Popup one which is ModalView like follwing

    
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.label import Label
    from kivy.uix.modalview import ModalView
    
    kv = Builder.load_string('''
    Screen:
        Button:
            text:'press to see text'
            on_press:app.add_floating_label()
    
    
    ''')
    
    
    class MyApp(App):
        view = None
    
        def build(self):
            return kv
    
        def open_floating_label(self):
            self.view = ModalView(auto_dismiss=True, size_hint=(None, None), background_color=[.1, .1, .1, 0],
                                  size=(300, 75))
            self.view.add_widget(Label(text="MESSAGE TEXT IS HERE!"))
            self.view.open()
        # you can also close the view using dismiss method
        def close_floating_label(self):
            self.view.close()
    
    
    MyApp().run()