Python KIVY:TextInput不允许我在里面写

时间:2018-02-19 22:28:27

标签: python kivy

我正在创建一个需要文本输入的应用程序(目的是创建一个图形shell,但无论如何)。当我尝试在文本输入中写任何东西时,没有任何反应,我无法在其中写入。 这是我的代码。如果要测试它以查看文本输入,则必须启动应用程序,然后单击其中一个“>”右边的绿色按钮,然后你点击“python 3 shell”。

的.py:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import BooleanProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.clock import Clock

from functools import partial

from kivy.config import Config
Config.set('graphics', 'minimum_width', 400)
Config.set('graphics', 'minimum_height', 300)
Config.write()


class SM(ScreenManager):

    def __init__(self, **kwargs):
        super(SM, self).__init__(**kwargs)

class Main(Screen):

    isShownMenu = BooleanProperty(True)
    view = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        Clock.schedule_once(self.create_scrollview)

    def create_scrollview(self, dt):
        base = ["connection {}".format(i) for i in range(40)]
        layout = GridLayout(cols=2, padding=10, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter("height"))

        for element in base:
            layout.add_widget(Button(text=element, size=(50, 50), size_hint=(1, None),
                                     background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1)))

            right_btn = Button(text=">", id=element, size=(50,50), size_hint=(None, None), background_color=(0, 1, 0, 1), color=(1, 1, 1, 1))
            right_btn.bind(on_press=partial(self.open_connected, right_btn))
            layout.add_widget(right_btn)

        scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        scrollview.add_widget(layout)
        self.ids.view.add_widget(scrollview)

    def open_connected(self, fun, name):
        self.manager.get_screen('connection_screen').ids.title.text = name.id
        self.manager.current = "connection_screen"

class Connection(Screen):

    isShownMenu = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(Connection, self).__init__(**kwargs)

class Py3Shell(Screen):

    isShownMenu = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(Py3Shell, self).__init__(**kwargs)


Builder.load_file("gui.kv")

class GUI(App):

    def build(self):
        return SM()

if __name__ == '__main__':
    GUI().run()

.kv:

#:kivy 1.10.0

<SM>:

    Main:
        name: "main_screen"
        id: main_screen

    Connection:
        name: "connection_screen"
        id: connection_screen

    Py3Shell:
        name: "py3shell_screen"
        id: py3shell_screen

<Main>:

    GridLayout:
        cols: 3

        Button:
            size: (8,8)
            size_hint: (None, 1)
            text: "|"
            background_color: (0,1,0,1)
            on_press: root.isShownMenu = not root.isShownMenu

        BoxLayout:
            id: menu
            orientation: 'vertical'
            width: 120 if root.isShownMenu else 0
            height: 120
            size_hint: (None, 1)
            opacity: 1 if root.isShownMenu else 0

            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"

        ScrollView:
            id:view

<Connection>:

    GridLayout:
        cols: 3

        Button:
            size: (8,8)
            size_hint: (None, 1)
            text: "|"
            background_color: (0,1,0,1)
            on_press: root.isShownMenu = not root.isShownMenu

        BoxLayout:
            id: menu
            orientation: 'vertical'
            width: 120 if root.isShownMenu else 0
            height: 120
            size_hint: (None, 1)
            opacity: 1 if root.isShownMenu else 0

            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"

        BoxLayout:
            orientation: 'vertical'

            GridLayout:
                cols: 2
                size: (50, 50)
                size_hint: (1, None)

                Button:
                    size: (50, 50)
                    size_hint: (None, None)
                    text: "<"
                    on_press: root.manager.current = "main_screen"

                Button:
                    id: title
                    size: (50, 50)
                    size_hint: (1, None)
                    background_color: (0, 0, 1, 1)
                    color: (1, 1, 1, 1)
                    text: ""


            BoxLayout:

                spacing: 10
                padding: 10

                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'top'

                    Button:
                        size: (80, 80)
                        size_hint: (1, None)
                        text: "Python 3 shell"
                        on_press: root.manager.current = "py3shell_screen"

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'top'

                    Button:
                        size: (80, 80)
                        size_hint: (1, None)
                        text: "System shell"


<Py3Shell>:

    GridLayout:
        cols: 3

        Button:
            text: "<"
            size: (50, 50)
            size_hint: (None, None)

        Button:
            text: "Python 3 shell"
            size: (50, 50)
            size_hint: (1, None)

        Button:
            text: "X"
            size: (50, 50)
            size_hint: (None, None)
            background_color: (1, 0, 0, 1)

    BoxLayout:

    GridLayout:
        cols: 3
        size: (50, 50)
        size_hint: (1, None)

        Label:
            text: "prompt $>"
            size: (75, 50)
            size_hint: (None, None)

        TextInput: # <----------------------------------------- here
            size: (50, 50)
            size_hint: (1, None)
            background_color: (.1, .1, .1, 1)
            color: (1, 1, 1, 1)

        Button:
            text: ">"
            size: (50, 50)
            size_hint: (None, None)
            background_color: (0, 1, 0, 1)

1 个答案:

答案 0 :(得分:1)

当我运行你的代码时,我收到了有关同名多个屏幕的警告。我认为那是因为您正在运行Builder.load_file("gui.kv")而忽略了返回的Main屏幕。所以我重写了你的gui.kv文件:

#:kivy 1.10.0


<Main>:
    name: "main_screen"
    id: main_screen

    GridLayout:
        id: glayout
        cols: 3

        Button:
            size: (8,8)
            size_hint_x: None
            text: "|"
            background_color: (0,1,0,1)
            on_press: main_screen.isShownMenu = not main_screen.isShownMenu

        BoxLayout:
            id: menu
            orientation: 'vertical'
            width: 120 if main_screen.isShownMenu else 0
            height: 120
            size_hint: (None, 1)
            opacity: 1 if main_screen.isShownMenu else 0

            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"

        ScrollView:
            id: view
            size_hint: 1, 1

<Connection>:
    name: "connection_screen"
    id: connection_screen

    GridLayout:
        cols: 3

        Button:
            size: (8,8)
            size_hint: (None, 1)
            text: "|"
            background_color: (0,1,0,1)
            on_press: connection_screen.isShownMenu = not connection_screen.isShownMenu

        BoxLayout:
            id: menu
            orientation: 'vertical'
            width: 120 if connection_screen.isShownMenu else 0
            height: 120
            size_hint: (None, 1)
            opacity: 1 if connection_screen.isShownMenu else 0

            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"
            Button:
                text: "Menu button"

        BoxLayout:
            id: blayout
            orientation: 'vertical'

            GridLayout:
                id: conlayout
                cols: 2
                size: (50, 50)
                size_hint: (1, None)

                Button:
                    size: (50, 50)
                    size_hint: (None, None)
                    text: "<"
                    on_press: connection_screen.manager.current = "main_screen"

                Button:
                    id: title
                    size: (50, 50)
                    size_hint: (1, None)
                    background_color: (0, 0, 1, 1)
                    color: (1, 1, 1, 1)
                    text: ""


            BoxLayout:

                spacing: 10
                padding: 10

                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'top'

                    Button:
                        size: (80, 80)
                        size_hint: (1, None)
                        text: "Python 3 shell"
                        on_press: connection_screen.manager.current = "py3shell_screen"

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'top'

                    Button:
                        size: (80, 80)
                        size_hint: (1, None)
                        text: "System shell"


<Py3Shell>:
    name: "py3shell_screen"
    id: py3shell_screen

    GridLayout:
        cols: 3

        Button:
            text: "<"
            size: (50, 50)
            size_hint: (None, None)

        Button:
            text: "Python 3 shell"
            size: (50, 50)
            size_hint: (1, None)

        Button:
            text: "X"
            size: (50, 50)
            size_hint: (None, None)
            background_color: (1, 0, 0, 1)

    BoxLayout:

    GridLayout:
        cols: 3
        size: (50, 50)
        size_hint: (1, None)

        Label:
            text: "prompt $>"
            size: (75, 50)
            size_hint: (None, None)

        TextInput: # <----------------------------------------- here
            id: ti
            height: 50
            size_hint: (1, None)
            background_color: (1, 1, 1, 1)
            color: (1, 1, 1, 1)

        Button:
            text: ">"
            size: (50, 50)
            size_hint: (None, None)
            background_color: (0, 1, 0, 1)

我仍然有不可编辑的TextInput,所以我添加了on_enter()方法来将焦点设置到TextInput小部件。并且不再需要SM类。这是修改后的.py文件:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import BooleanProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.clock import Clock

from functools import partial

from kivy.config import Config
Config.set('graphics', 'minimum_width', 400)
Config.set('graphics', 'minimum_height', 300)
Config.write()


class Main(Screen):

    isShownMenu = BooleanProperty(True)
    view = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        Clock.schedule_once(self.create_scrollview)

    def create_scrollview(self, dt):
        base = ["connection {}".format(i) for i in range(40)]
        layout = GridLayout(cols=2, padding=10, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter("height"))

        for element in base:
            layout.add_widget(Button(text=element, size=(50, 50), size_hint=(1, None),
                                     background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1)))

            right_btn = Button(text=">", id=element, size=(50,50), size_hint=(None, None), background_color=(0, 1, 0, 1), color=(1, 1, 1, 1))
            right_btn.bind(on_press=partial(self.open_connected, right_btn))
            layout.add_widget(right_btn)

        scrollview = self.ids['view']
        scrollview.add_widget(layout)

    def open_connected(self, fun, name):
        self.manager.get_screen('connection_screen').ids.title.text = name.id
        self.manager.current = "connection_screen"


class Connection(Screen):

    isShownMenu = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(Connection, self).__init__(**kwargs)

class Py3Shell(Screen):

    isShownMenu = BooleanProperty(True)

    def __init__(self, **kwargs):
        super(Py3Shell, self).__init__(**kwargs)

    def on_enter(self, *args):
        super().on_enter(*args)
        self.ids.ti.focus = True


class GUI(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(Main())
        sm.add_widget(Connection())
        sm.add_widget(Py3Shell())
        return sm

if __name__ == '__main__':
    GUI().run()