我试图在气泡中为Kivy中的TextInput显示数字键盘。可能吗?
到目前为止,我有:

Builder.load_string('''
<NumericKeyboard>
size_hint: (None, None)
size: (160, 120)
pos_hint: {'center_x': .5, 'y': .6}
BubbleButton:
    text: 'Cut'
BubbleButton:
    text: 'Copy'
BubbleButton:
    text: 'Paste'
''')

class NumericKeyboard(Bubble):
    pass

class CustomTextInput(TextInput):
def __init__(self, **kwargs):
    super(CustomTextInput, self).__init__(**kwargs)

def on_focus(self, instance, value):
    self.bubb = NumericKeyboard()
    self.add_widget(self.bubb)


但是气泡不会显示。

最佳答案

是的,可以使用Kivy Bubble for TextInput小部件显示数字键盘。有关详细信息,请参见以下示例。

注意:文本输入未过滤。



main.py

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.bubble import Bubble, BubbleButton
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder


class CustomBubbleButton(BubbleButton):
    pass


class NumericKeyboard(Bubble):
    layout = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(NumericKeyboard, self).__init__(**kwargs)
        self.create_bubble_button()

    def create_bubble_button(self):
        numeric_keypad = ['7', '8', '9', '4', '5', '6', '1', '2', '3', '', '0', '.']
        for x in numeric_keypad:
            if len(x) == 0:
                self.layout.add_widget(Label(text=""))
            else:
                bubb_btn = CustomBubbleButton(text=str(x))
                self.layout.add_widget(bubb_btn)


class BubbleShowcase(FloatLayout):
    text_input = ObjectProperty(None)

    def show_bubble(self, *l):
        if not hasattr(self, 'bubb'):
            self.bubb = bubb = NumericKeyboard()
            self.bubb.arrow_pos = "bottom_mid"
            self.add_widget(bubb)


Builder.load_file("test.kv")


class TestBubbleApp(App):
    title = "Numeric Key Pad - Using Bubble"

    def build(self):
        return BubbleShowcase()


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


测试文件

#:kivy 1.10.0

<CustomBubbleButton>:
    on_release:
        app.root.text_input.text += self.text


<NumericKeyboard>:
    layout: layout

    size_hint: (None, None)
    size: (160, 120)
    pos_hint: {'center_x': .5, 'y': .6}

    GridLayout:
        id: layout
        cols: 3

<BubbleShowcase>:
    text_input: text_input

    canvas:
        Color:
            rgba: 0, 1, 1, 1
        Rectangle:
            size: self.width, self.height
    TextInput:
        id: text_input
        pos_hint: {'center_x': .5, 'y': .54}
        size_hint: (0.2, 0.06)
        cursor_blink: True
        font_size: 20
        multiline: False
        on_focus:
            root.show_bubble()


输出量

python - Kivy Python TextInput显示气泡-LMLPHP
python - Kivy Python TextInput显示气泡-LMLPHP
python - Kivy Python TextInput显示气泡-LMLPHP
python - Kivy Python TextInput显示气泡-LMLPHP
python - Kivy Python TextInput显示气泡-LMLPHP

关于python - Kivy Python TextInput显示气泡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47552735/

10-14 18:20
查看更多