Changing window size seems to affect string manipulation

16 views
Skip to first unread message

ST

unread,
Aug 2, 2020, 10:47:16 PM8/2/20
to Kivy users support
Hello, I am puzzled and I would like to ask for your help.

I have a program where it adds a suffix to an entered text.
However, it somehow only works for strings that are less than 49 characters long.

I've created a minimal working example (see attachment or end of post).
The program works like this:
  1. User enters text into textbox, adjusts window size, then presses button
  2. Add '_suffix' to the end of the text string
  3. Remember window size
  4. Change window size to (400,400) and display popup
  5. User presses 'Print' -> print modified string
  6. User presses 'Cancel' -> close popup, return window to original size
For example, 

    input: 123456789012345678901234567890123456789012345678
    output: 123456789012345678901234567890123456789012345678_suffix

However, for some reason, the suffix is not added for strings more than 48 characters long.

I found that deleting 
        # set popup window size
        Window.size = (400, 400)

can get rid of this issue, but I want to keep this feature.

I've tried to increase the wait time using Clock, but it didn't work.

I found that double-clicking the 'Open popup' button before the popup is opened
causes this error to go away, if the window size is not changed.
But I can't understand why.

Any help is appreciated. Thanks!

Below is the code:

test.py

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()
test.py

Elliot Garbus

unread,
Aug 2, 2020, 11:56:47 PM8/2/20
to kivy-...@googlegroups.com

There were are few things I changed below, the issue was related to a line in the kv file..

input_text: input_text.text 

 

 

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  # Not sure what you are trying to do here...
)


(FloatLayout):
    input_text = StringProperty(
''# removed None

    #Window.size = (600, 400)        # Moved to build, this code was not in a method.
   
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
):
        Window.size =
600, 400  # moved here
       
return MainScreen()


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/21943552-08b1-4f1c-aed2-b65e0d11a962o%40googlegroups.com.

 

ST

unread,
Aug 3, 2020, 12:49:14 AM8/3/20
to Kivy users support
Dear Elliot Garbus,

Thank you for the quick response! It worked perfectly.

I realized I had ObjectProperty and StringProperty all mixed up, hence the line:
input_text: input_text.text

I've also revised my code to get rid of the self.ids method, using ObjectProperty instead, as recommended in the Kivy Programming Guide.

I feel I have gained a better understanding of the Kivy language. Thank you so much.
 
revised code:

from kivy.app import App
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

    input_text = ObjectProperty(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.modified_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)

        self._popup.open()
        
    def add_suffix(self):
    
        # add suffix
        self.modified_text = self.input_text.text + '_suffix'

        self.open_popup()

    def do_stuff(self):
        '''Action when "Run" button is pressed'''
        
        # do stuff here

        self.add_suffix()

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

    def build(self):
        Window.size = 600, 400
        return MainScreen()

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

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages