简体   繁体   English

使用FloatLayout在Kivy中重叠TextInputs

[英]Overlapping TextInputs in Kivy using FloatLayout

Using this code: 使用此代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput


class Main(App):
    def build(self):
        root = FloatLayout(size=(100, 100))
        root.add_widget(TextInput(pos=(0, 0)))
        root.add_widget(TextInput(pos=(50, 50)))
        return root


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

I get two TextInput s, one on top of the other. 我得到两个TextInput ,一个在另一个之上。 When I click on the top TextInput (by clicking somewhere in the middle of the screen), the focus goes to the lower TextInput for some reason. 当我单击顶部的TextInput (通过单击屏幕中间的某个位置),由于某种原因,焦点移到了下部的TextInput In fact, the only way I can get focus on the top TextInput is by clicking entirely outside of the lower TextInput (by clicking right at the top of the screen). 实际上,我可以将注意力集中在顶部TextInput上的唯一方法是完全单击下部TextInput外部(通过单击屏幕顶部的右侧)。 Why does this happen, and how can I circumvent this? 为什么会发生这种情况,我该如何规避呢?

Your problem can be approached in two ways. 您的问题可以通过两种方式解决。 Float layout honors the pos_hint and the size_hint properties of its children.So you need to set size_hint for textinput. 浮动布局使用pos_hint及其子元素的size_hint属性,因此需要为文本输入设置size_hint。

ie- 即 -

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput


class Main(App):
    def build(self):
        root = FloatLayout(size=(100, 100))
        root.add_widget(TextInput(pos=(0, 0),size_hint=(0.5,0.5)))
        root.add_widget(TextInput(pos=(100, 100),size_hint=(0.5,0.5)))
        return root


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

or use boxlayout instead of floatlayout 或使用boxlayout而不是floatlayout

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


class Main(App):
    def build(self):
        root = BoxLayout(size=(100, 100))
        root.add_widget(TextInput(pos=(0, 0)))
        root.add_widget(TextInput(pos=(50, 50)))
        return root


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM