使 Kivy TextInput 框架不可见但显示文本

Posted

技术标签:

【中文标题】使 Kivy TextInput 框架不可见但显示文本【英文标题】:Make Kivy TextInput Frames invisible but show text 【发布时间】:2018-05-16 12:53:49 【问题描述】:

我正在尝试使用

使 TextInput 小部件像您一样“不可见”
opacity: 0

但是,我希望显示 TextInput 中的文本。如果我使用

opacity: 0

TextInput 小部件和小部件内的文本不可见,有没有办法在显示文本的同时“隐藏”小部件?

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class ExampleScreen(Screen):
    pass


class ExampleManager(ScreenManager):
    pass


root_widget = Builder.load_string('''
ExampleManager:
    ExampleScreen:

<ExampleScreen>:
    name: 'example_screen'
    TextInput:
        id: t_1
        text: 'Test text in input 1'
        size_hint: .5, .5
        pos_hint: 'x': 0, 'y': .5
        multiline: True
        readonly: True
        opacity: 0  ## If I remove this line both text edits are visible. 
                    ##You can test this by putting ## before opacity

        ## What I'm trying to do, is make the TextInput widget opaque so 
        ##that you can't see the frame and only the text is visible

    TextInput:
        id: t_2
        text: 'Test text in input 2'
        size_hint: .5, .5
        pos_hint: 'x': 0, 'y': 0
        multiline: True
        readonly: True
        opacity: 0  ## If I remove this line both text edits are visible. 
                    ##You can test this by putting ## before opacity

        ## What I'm trying to do, is make the TextInput widget opaque so 
        ##that you can't see the frame and only the text is visible

''')

class TestWidgetsApp(App):
    def build(self):
        self.title = 'Proj'
        return root_widget

TestWidgetsApp().run()

【问题讨论】:

考虑设置 ExampleScreen 的背景颜色并将 TextInput 的背景颜色设置为相同的颜色 如果您不想设置 background_color,您也可以将 background_normal 和 background_active 图像设置为 '' 【参考方案1】:

使用background_color属性并将alpha通道设置为0:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class ExampleScreen(Screen):
    pass


class ExampleManager(ScreenManager):
    pass


root_widget = Builder.load_string('''
ExampleManager:
    ExampleScreen:

<ExampleScreen>:
    name: 'example_screen'
    TextInput:
        id: t_1
        text: 'Test text in input 1'
        size_hint: .5, .5
        pos_hint: 'x': 0, 'y': .5
        multiline: True
        readonly: True
        foreground_color: (1,0,1,1)
        background_color: (0,0,0,0)

    TextInput:
        id: t_2
        text: 'Test text in input 2'
        size_hint: .5, .5
        pos_hint: 'x': 0, 'y': 0
        multiline: True
        readonly: True
        foreground_color: (1,1,0,1)
        background_color: (0,0,0,0)

''')

class TestWidgetsApp(App):
    def build(self):
        self.title = 'Proj'
        return root_widget

TestWidgetsApp().run()

【讨论】:

以上是关于使 Kivy TextInput 框架不可见但显示文本的主要内容,如果未能解决你的问题,请参考以下文章

TextInput中的kivy ScrollView实现

如何使用kivy将textinput转换为float并返回

为啥不可能在 kivy 中制作 Markup TextInput?

无法在 KIVY Python 中打印取自 kivy.uix.textinput.TextInput 的文本

Kivy:如何在 kivy 中制作圆角 TextInput?

如何使可编辑标签在kivy中工作