mirror of https://github.com/kivy/kivy.git
Add on_textedit event to SDL2 Window (#5597)
* Add on_textrdit event * Add on_textedit event * Add TextEdit Event * Add on_textedit sample
This commit is contained in:
parent
a432e0d73a
commit
87897c489d
|
@ -0,0 +1,104 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
on_textedit event sample.
|
||||
'''
|
||||
from kivy.app import App
|
||||
from kivy.uix.widget import Widget
|
||||
from kivy.lang import Builder
|
||||
from kivy.properties import StringProperty
|
||||
from kivy.core.text import LabelBase, DEFAULT_FONT
|
||||
from kivy.uix.textinput import TextInput
|
||||
from kivy.base import EventLoop
|
||||
|
||||
|
||||
class TextInputIME(TextInput):
|
||||
testtext = StringProperty()
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(TextInputIME, self).__init__(**kwargs)
|
||||
EventLoop.window.bind(on_textedit=self._on_textedit)
|
||||
|
||||
def _on_textedit(self, window, text):
|
||||
self.testtext = text
|
||||
|
||||
|
||||
class MainWidget(Widget):
|
||||
text = StringProperty()
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(MainWidget, self).__init__(**kwargs)
|
||||
self.text = ''
|
||||
|
||||
def confim(self):
|
||||
self.text = self.ids["text_box"].text
|
||||
|
||||
def changeFont(self):
|
||||
try:
|
||||
LabelBase.register(DEFAULT_FONT, self.ids["text_font"].text)
|
||||
except Exception:
|
||||
self.ids["text_font"].text = "can't load font."
|
||||
|
||||
|
||||
class TextEditTestApp(App):
|
||||
def __init__(self, **kwargs):
|
||||
super(TextEditTestApp, self).__init__(**kwargs)
|
||||
|
||||
def build(self):
|
||||
return MainWidget()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
Builder.load_string('''
|
||||
<MainWidget>:
|
||||
BoxLayout:
|
||||
orientation: 'vertical'
|
||||
size: root.size
|
||||
BoxLayout:
|
||||
Label:
|
||||
size_hint_x: 3
|
||||
text: "Multi language font file path"
|
||||
TextInput:
|
||||
id: text_font
|
||||
size_hint_x: 5
|
||||
Button:
|
||||
size_hint_x: 2
|
||||
text: "Change Font"
|
||||
on_press: root.changeFont()
|
||||
BoxLayout:
|
||||
Label:
|
||||
size_hint_x: 3
|
||||
text: "Text editing by IME"
|
||||
Label:
|
||||
size_hint_x: 7
|
||||
text:text_box.testtext
|
||||
canvas.before:
|
||||
Color:
|
||||
rgb: 0.5765 ,0.5765 ,0.5843
|
||||
Rectangle:
|
||||
pos: self.pos
|
||||
size: self.size
|
||||
BoxLayout:
|
||||
Label:
|
||||
size_hint_x: 3
|
||||
text: "Enter text ->"
|
||||
TextInputIME:
|
||||
id: text_box
|
||||
size_hint_x: 7
|
||||
focus: True
|
||||
BoxLayout:
|
||||
Button:
|
||||
size_hint_x: 3
|
||||
text: "Confirm text property"
|
||||
on_press: root.confim()
|
||||
Label:
|
||||
size_hint_x: 7
|
||||
text: root.text
|
||||
canvas.before:
|
||||
Color:
|
||||
rgb: 0.5765 ,0.5765 ,0.5843
|
||||
Rectangle:
|
||||
pos: self.pos
|
||||
size: self.size
|
||||
''')
|
||||
TextEditTestApp().run()
|
|
@ -321,6 +321,13 @@ class WindowBase(EventDispatcher):
|
|||
You can listen to this one, and clean whatever you can.
|
||||
|
||||
.. versionadded:: 1.9.0
|
||||
|
||||
`on_textedit(self, text)`:
|
||||
Fired when inputting with IME.
|
||||
The string inputting with IME is set as the parameter of
|
||||
this event.
|
||||
|
||||
.. versionadded:: 1.10.1
|
||||
'''
|
||||
|
||||
__instance = None
|
||||
|
@ -860,7 +867,8 @@ class WindowBase(EventDispatcher):
|
|||
'on_key_down', 'on_key_up', 'on_textinput', 'on_dropfile',
|
||||
'on_request_close', 'on_cursor_enter', 'on_cursor_leave',
|
||||
'on_joy_axis', 'on_joy_hat', 'on_joy_ball',
|
||||
'on_joy_button_down', 'on_joy_button_up', 'on_memorywarning')
|
||||
'on_joy_button_down', 'on_joy_button_up', 'on_memorywarning',
|
||||
'on_textedit')
|
||||
|
||||
def __new__(cls, **kwargs):
|
||||
if cls.__instance is None:
|
||||
|
@ -1740,6 +1748,15 @@ class WindowBase(EventDispatcher):
|
|||
'''
|
||||
pass
|
||||
|
||||
def on_textedit(self, text):
|
||||
'''Event called when inputting with IME.
|
||||
The string inputting with IME is set as the parameter of
|
||||
this event.
|
||||
|
||||
.. versionadded:: 1.10.1
|
||||
'''
|
||||
pass
|
||||
|
||||
@reify
|
||||
def dpi(self):
|
||||
'''Return the DPI of the screen. If the implementation doesn't support
|
||||
|
|
|
@ -669,6 +669,10 @@ class WindowSDL(WindowBase):
|
|||
text = args[0]
|
||||
self.dispatch('on_textinput', text)
|
||||
|
||||
elif action == 'textedit':
|
||||
text = args[0]
|
||||
self.dispatch('on_textedit', text)
|
||||
|
||||
# unhandled event !
|
||||
else:
|
||||
Logger.trace('WindowSDL: Unhandled event %s' % str(event))
|
||||
|
|
Loading…
Reference in New Issue