0

I want to resize the text input as it fills up half of the screen.

This code fills up half of the screen:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label 
from kivy.uix.textinput import TextInput 

class MyApp(App): 
   def build(self): 
     self.box = BoxLayout() 
     self.label = Label(text="Hi there, Welcome.") 
     self.txt = TextInput(text="Hello World")
     self.box.add_widget(self.label) 
     self.box.add_widget(self.txt) 
     return self.box 

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

I googled some with the same issue and I saw this: Python kivy - how to reduce height of TextInput tried one from the answer:

Trying to resize it like this, but doesn't work, (not really sure how's the right way) Kivy launcher will close right after tapping it:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label 
from kivy.uix.textinput import TextInput 

class MyApp(App): 
   def build(self): 
     self.box = BoxLayout() 
     self.label = Label(text="Hi there, Welcome.") 
     self.txt = TextInput()
     TextInput:
         size_hint: (.2, None)
         height: 30 
         multiline: False 
         text: "hello world"
     self.box.add_widget(self.label) 
     self.box.add_widget(self.txt) 
     return self.box 

if __name__ == "__main__":
    MyApp().run()        
yalpsid
  • 15
  • 3

1 Answers1

0

Using Python code

The following is the kv language converted into Python code.

 self.txt = TextInput(size_hint=(.2, None), height=30, multiline=False, text="hello world")

Using Kivy Builder

The following solution illustrates using Kivy Builder to load the kv language.

Snippets - main

from kivy.lang import Builder


Builder.load_string("""
<TextInput>:
    size_hint: (.2, None)
    height: 30
    multiline: False
    text: "hello world"
""")

Example

The following example contains both solution but one of it is commented out.

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang import Builder


Builder.load_string("""
<TextInput>:
    size_hint: (.2, None)
    height: 30
    multiline: False
    text: "hello world"
""")


class MyApp(App):
    def build(self):
        self.box = BoxLayout()
        self.label = Label(text="Hi there, Welcome.")
        # self.txt = TextInput(size_hint=(.2, None), height=30, multiline=False, text="hello world")
        self.txt = TextInput()
        self.box.add_widget(self.label)
        self.box.add_widget(self.txt)
        return self.box


if __name__ == "__main__":
    MyApp().run()
ikolim
  • 15,721
  • 2
  • 19
  • 29