Kivy Popup handling

28 views
Skip to first unread message

Dragan Mestrovik

unread,
Feb 16, 2018, 1:27:50 AM2/16/18
to Kivy users support
Hi,

I am new to App development, Kivy and Python. I tried to show a popup on a button click. When click "Cancel" button on popup, it should get dismissed. I think there is a mistake in my code. When i clicked on 'Cancel' button on popup, nothing happening. Can anyone help me?

import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup

class NotesMainScreen(FloatLayout):
def Download(self,btn):
DownloadCompletePopup().run()
def __init__(self,**kwargs):
super(NotesMainScreen, self).__init__(**kwargs)
self.add_widget(Label(text="Welcome ", pos=(100, 300), size_hint = (.1,.05)))
self.Url = TextInput(multiline = False, pos=(200, 300), size_hint = (.1,.05))
self.add_widget(self.Url)
btnRun = Button(text="Run", pos=(300,300), size_hint = (.1,.05))
btnRun.bind(on_press = self.Download)
self.add_widget(btnRun)

class DownloadCompletePopup(App):
    #override the build method and return the root widget of this App
    def build(self):
        # Define a layout for this App
        self.layout = FloatLayout()            
        popupLabel  = Label(text  = "Hoorey Completed!!", pos=(300,300), size_hint = (.3,.1))
        closeButton = Button(text = "Close", pos=(320,250), size_hint = (.2,.2))
        self.layout.add_widget(popupLabel)
        self.layout.add_widget(closeButton)       

        # Instantiate the modal popup and display
        popup = Popup(title='Donwload Completed', content=self.layout, pos=(300,300), size_hint=(.4, .3), auto_dismiss=False)  
# Attach close button press with popup.dismiss action
        closeButton.bind(on_press = popup.dismiss) 
        popup.open()         
class Notes(App):
def build(self):
return NotesMainScreen()
if __name__ == '__main__':
Notes().run()



Dragan Mestrovik

unread,
Feb 16, 2018, 3:28:41 AM2/16/18
to Kivy users support
I fixed this by moving popup related code to main class.

Jacek Blocki

unread,
Feb 17, 2018, 11:22:16 AM2/17/18
to Kivy users support
App.build() is run before actual application start, so event binding in this method does not work since EventDispatcher structures are not yet initialized.
JB
Reply all
Reply to author
Forward
0 new messages