Kivy Python TextInput display Bubble(Kivy Python TextInput 显示气泡)
本文介绍了Kivy Python TextInput 显示气泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to display numeric keyboard in bubble for TextInput in Kivy. Is it possible? So far I have:
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)
But the bubble will not display.
解决方案
Yes, it is possible to display numeric key pad using Kivy Bubble for TextInput widget. Please refer to the example below for details.
Note: The text input is not filtered.
Example
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()
test.kv
#: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()
Output
这篇关于Kivy Python TextInput 显示气泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Kivy Python TextInput 显示气泡
基础教程推荐
猜你喜欢
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01