Kivy Python文本输入显示Bubb

2024-10-03 09:11:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在泡泡中显示数字键盘,以便在Kivy中输入文本。有可能吗? 到目前为止,我已经:

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)

但泡沫不会出现。在


Tags: textselfnonesizeinitdefkwargsclass
2条回答

不能将小部件添加到文本输入,因为它不是布局。您应该将文本输入添加到布局中,然后将气泡添加到此布局中。试试这个:

from kivy.app import App
from kivy.uix.bubble import Bubble
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

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 CustomTextInputLayout(FloatLayout):
    def __init__(self, **kwargs):
        super(CustomTextInputLayout, self).__init__(**kwargs)
        self.add_widget(CustomTextInput(self))


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

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

class MyTestApp(App):
    def build(self):
        return CustomTextInputLayout()

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

是的,可以使用Kivy Bubble for TextInput小部件显示数字键盘。详情请参考下面的例子。在

注意:文本输入不会被过滤。在

示例

在主.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()

在试验电压在

^{pr2}$

输出

Figure 1 - App StartupFigure 2 - Bubble Numeric Key Pad on_focus TextInputFigure 3 - Pressed Digit 3Figure 4 - Pressed Decimal PointFigure 5 - Pressed Digits 1, 4, `, 5, and 9

相关问题 更多 >