Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widgets ( button , inputtext etc. ) inside screen is not working

Tags:

python

kivy

I am trying to put some text box and button inside screen but seems that it doesn't work. Any suggestion or issue in code? when running main.py the textinput and first button and label is not there

Phone.kv

<Phone>:
    orientation: 'vertical'
    ScreenManager:
        size_hint: 1, 1
        id: _screen_manager
        Screen:
            name: 'screen1'
            Label:
                markup: True
                text: 'manish'
            TextInput:
                text: 'Hi Kivy'
            Button:
                text: 'Go to Screen 1'
                on_press:
                    _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press:
                    root.login()
                    _screen_manager.current = 'screen2'

        Screen:
            name: 'screen2'
            GridLayout:
                cols: 3
                padding: 50
                Button:
                    text: "1"

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
import time

class Phone(FloatLayout):
    def login(self):
        print "before"
        time.sleep(2)
        print "after"

class PhoneApp(App):
    def build(self):
        return Phone()

if __name__ == '__main__':
    PhoneApp().run()
like image 709
pkm Avatar asked Apr 25 '26 08:04

pkm


1 Answers

Everything is probably there,
just all on top of each other since Screen by default places its children to fill itself.

Try something like the following,
which uses a BoxLayout that should place its children in different places.

Screen:
    name: 'screen1'
    BoxLayout:
        orientation: 'vertical'
        Label:
            markup: True
            text: 'manish'
        TextInput:
            text: 'Hi Kivy'
        Button:
            text: 'Go to Screen 1'
            on_press:
                _screen_manager.current = 'screen1'
        Button:
            text: 'Go to Screen 2'
            on_press:
                root.login()
                _screen_manager.current = 'screen2'
like image 77
inclement Avatar answered Apr 27 '26 22:04

inclement