首页 > 解决方案 > 文本输入突然停止在 Kivy 应用程序中工作

问题描述

我正在构建一个带有几个屏幕的简单 Kivy 应用程序。第一个屏幕有几个按钮,点击后会移动到第二个屏幕。第二个屏幕有一个文本输入小部件和一个浮动布局内的按钮。布局通过 Builder 显式调用 kv 文件作为根小部件加载。

一切正常,我在文本输入属性中添加了一个“焦点:真”标签。该应用程序运行良好,我可以在文本输入字段中键入焦点设置为 True。但是,文本输入字段突然停止工作,代码或布局没有任何变化。我不确定并在 Google 上搜索了几个可能的解决方案,但都没有奏效:

  1. 删除了“焦点:真”属性并重新加载了应用程序,但文本输入字段仍然没有响应。我无法从键盘上输入任何内容。

  2. 另一篇文章指出,kv 文件被加载了两次,导致行为不稳定。我试图删除显式的 Builder 文件调用并在主代码中返回根小部件(屏幕管理器)。但是,它弄乱了我的整个应用程序,只显示一个黑色的空屏幕。

你能告诉我我可能做错了什么吗?代码如下:

蟒蛇代码:

from kivy.config import Config
Config.set('kivy','window_icon','sivaicon.png')
Config.set('graphics', 'resizable', True)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.lang.builder import Builder


class SivaLoginScreen(Screen):
    def twitter_authentication(self):
        App.get_running_app().root.current='verify_screen'

    def linkedin_authentication(self):
        App.get_running_app().root.current='verify_screen'


class SivaVerifyScreen(Screen):
    pass


class SivaTabbedScreen(Screen):
    pass


class SivaScreenManager(ScreenManager):
    pass


class ImageButton(ButtonBehavior, Image):
    pass


# Tell Kivy to directly load a file. If this file defines a root widget, it will be returned by the method.
root_widget = Builder.load_file('siva.kv')

class SivaApp(App):
    def build(self):
        # Initialize root widget
        return root_widget


if __name__ == '__main__':
    # Run application
    SivaApp().run()

.kv 文件:

SivaScreenManager:
    SivaLoginScreen:
    SivaVerifyScreen:
    SivaTabbedScreen:


<ImageButton>:
    keep_ratio: True


<SivaLoginScreen>:
    name: 'login_screen'
    canvas.before:
        Color:
            rgba: 195/255, 60/255, 35/255, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        size: root.width, root.height
        Image:
            id: login_logo_siva
            source: 'images/sivalogo1.png'
            keep_ratio: True
            size_hint: 0.3, 0.3
            pos_hint: {'center_x':0.5, 'center_y':0.75}
        Label:
            id: login_label_siva
            pos: self.x*0.5-4, self.y*0.5+15
            markup: True
            font_name: 'roboto/Roboto-Medium.ttf'
            text: '[color=#FDFD98]S.[/color][color=#B29DD9]I[/color][color=#FDFD98].[/color][color=#77DD77]V[/color][color=#FDFD98].[/color][color=#779ECB]A[/color]'
            font_size: '40sp'
        Label:
            id: login_label_slogan1
            pos: self.x*0.5-3, self.y*0.5-6
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]SLOGAN TEXT[/color]'
            font_size: '13sp'
        Label:
            id: login_label_slogan2
            pos: self.x*0.5-3, self.y*0.5-20
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: '[color=#FDFD98]HEADLINE TEXT[/color]'
            font_size: '13sp'
        BoxLayout:
            id:login_button_layout
            orientation: 'horizontal'
            size_hint: 0.2, 0.2
            pos_hint: {'center_x':0.5, 'center_y':0.25}
            ImageButton:
                id: twitter_button
                source: {'normal': 'images/twitter-96.png', 'down': 'images/twitter-96.png'} [self.state]
                on_release: root.twitter_authentication()
            ImageButton:
                id: linkedin_button
                source: {'normal': 'images/linkedin-96.png', 'down': 'images/linkedin-96.png'} [self.state]
                on_release: root.linkedin_authentication()


<SivaVerifyScreen>:
    name: 'verify_screen'
    canvas.before:
        Color:
            rgba: 195/255, 60/255, 35/255, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        size: root.width, root.height
        Label:
            id: verify_label
            markup: True
            font_name: 'roboto/Roboto-Regular.ttf'
            text: 'Paste the verification code'
            font_size: '16sp'
            pos_hint: {'center_x':0.5, 'center_y':0.7}
            size_hint: 1, 0.4
        TextInput:
            id: verify_input
            multiline: False
            font_size: '30sp'
            pos_hint: {'center_x':0.5, 'center_y':0.55}
            size_hint: 0.5, 0.1
        ImageButton:
            id: verify_button
            source: {'normal': 'images/lock-96.png', 'down': 'images/lock-96.png'} [self.state]
            pos_hint: {'center_x':0.5, 'center_y':0.35}
            size_hint: 0.5, 0.5


<SivaTabbedScreen>:
    name: 'tabbed_screen'
    FloatLayout:
        size: root.width, root.height
        Label:
            pos: self.x*0.5, self.y*0.5
            text: 'SECOND SCREEN'
            font_size: '50sp'

请指教。我无助地卡住了。:(

提前致谢

标签: python-3.xkivykivy-languagetextinput

解决方案


好的,这就是问题所在:我的一些小部件相互重叠,从而使下面的小部件无响应。就我而言,一个按钮小部件与我的 textinput 小部件重叠,因此我无法在 textinput 小部件中输入文本。

我使用 Kivy 检查器工具来确定小部件尺寸的范围:

python3 main.py -m inspector

在应用程序运行时使用 Ctrl+e 启动检查器,然后单击每个小部件以检查其大小、位置和父级。我减小了按钮小部件的大小并将浮动布局转换为堆叠框布局,这解决了问题。


推荐阅读