from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import NumericProperty, ObjectProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup

Builder.load_string("""

#:kivy 1.11.1

<MainScreen>:

    input_text: input_text.text

    BoxLayout:
        orientation: 'vertical'

        TextInput:
            id: input_text
            
        Button:
            text: 'Open popup'
            on_release: root.do_stuff()

<PopupScreen>

    BoxLayout:
        orientation : 'vertical'
        size: root.size
        pos: root.pos
        
        Label:
            size_hint_y: 0.3
            text: 'Message'
                
        Button:
            size_hint_y: 0.5
            text: 'Print'
            on_release: root.print_text()
            
        Button:
            size_hint_y: 0.2
            text: 'Cancel'
            on_release: root.cancel()

""")


class PopupScreen(FloatLayout):

    print_text = ObjectProperty(None)
    cancel = ObjectProperty(None)

class MainScreen(FloatLayout):

    Window.size = (600, 400)
    
    input_text = StringProperty(None)
    
    def dismiss_popup(self):
        self._popup.dismiss()
        Window.size = self.current_size # return window to adjusted size

    def print_text(self):

        print("output: " + self.input_text)

    def open_popup(self):
    
        # remember main screen window size
        self.current_size = Window.size
        
        # set popup window size
        Window.size = (400, 400)

        # open popup
        content = PopupScreen(print_text=self.print_text, cancel=self.dismiss_popup)
        self._popup = Popup(title="Popup", content=content)
        
        print("check3: " + self.input_text)
        
        Clock.schedule_once(lambda dt: self._popup.open(), 0.5)
        
    def add_suffix(self):
    
        # refresh text (to prevent _suffix_suffix when rerun without editing text)
        self.input_text = self.ids.input_text.text
    
        # add suffix
        self.input_text = self.input_text + '_suffix'

        print("check2: " + self.input_text)

        Clock.schedule_once(lambda dt: self.open_popup(), 0.5)

    def do_stuff(self):
        '''Action when "Run" button is pressed'''
        
        #do stuff here
        
        print("check1: " + self.input_text)

        self.add_suffix()

# define Base Class of Kivy App
class TestApp(App):

    def build(self):
        return MainScreen()

# run program
if __name__ == '__main__': 
    TestApp().run()