- Kivy Cookbook
- Hugo Solis
- 599字
- 2021-07-16 20:39:57
The differences between the touch and motion events
There is a key difference between touch and motion events. A motion event is a continuous succession of many touch events. However, we also know that a touch event always has the pos
profile, namely position information. The motion event, however, is not dispatched throughout the widget tree.
Getting ready
In this recipe, we will use the Kv language for the design of the widgets, so we assume that the reader is familiar with the Kv language or did the lecture of the first chapter. Also, this recipe will use the common button widget for reference.
How to do it…
Use this recipe and follow these steps:
- First, in the KV file, define a button:
<MyW>: Button: id: button1 pos: 0,0 text: 'Hello'
- In the class of the widget in the Python code, we need to override the method
on_touch_move
. - Change the button's position with the information in
touch.pos
:import kivy kivy.require('1.9.0') from kivy.app import App from kivy.uix.widget import Widget class MyW(Widget): def on_touch_move(self, touch): if 'pos' in touch.profile: self.ids.button1.pos = touch.pos class e5App(App): def build(self): return MyW() if __name__ == '__main__': e5App().run()
How it works…
Let's start with the KV file, the first line is the name of the rule, similar to the class in the Python file. The second line is the definition of the button widget, the third line is the ID of the button, which is necessary to do the call of the widget inside the Python code, the fourth line specifies the initial position of the button, and the fifth line is the definition of the text in the button.
In the Python code, the initial four lines are usual to use Kivy and the button widget. The fifth one:
class MyW(Widget):
It is where we define the class associated with our rule in the KV file. The sixth line:
def on_touch_move(self, touch):
Here, we start the declaration of the dispatched on_touch_move
method; you must note the parameter touch in the declaration, as this parameter is necessary to call the event using the input. Now the seventh line:
if 'pos' in touch.profile:
This is a verification line, because we need to be sure that the input device used in the platform, where we are running our code, supports the button profile. If this line is not present when the next one is performed, the app could crash. The eighth line:
self.ids.button1.pos = touch.pos
This line is where we do the call to the profile position, which gives the information of where the touch occurs, and we changed the position of the button with the ID button1
with that information. The last lines of the Python code are usual to run and display our Kivy interface. Just remember that the initial part of the name of the class is:
class e5App(App):
It will be same as the KV file; the name of the KV file in this case is e5.kv
.
The last thing to remark is that we do not limit the input event to the button, so the touch can occur anywhere in the window.
There's more…
Also, the motion events are compatible with the touch event. It is possible to add to our code to the class MyW
a on_touch_down
method like this:
def on_touch_down(self, touch): if 'button' in touch.profile: self.ids.button1.text = touch.button
With this addition, now when you touch the text inside, the button is going to change to the information about which button is used.
See also
To get more details about widgets, see the recipes in Chapter 4, Widgets.
- Mobile Web Performance Optimization
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- JavaScript:Functional Programming for JavaScript Developers
- Apache Hive Essentials
- Java開發入行真功夫
- 基于差分進化的優化方法及應用
- 高級C/C++編譯技術(典藏版)
- Learning Apache Kafka(Second Edition)
- jQuery開發基礎教程
- Java:High-Performance Apps with Java 9
- Flutter跨平臺開發入門與實戰
- 打開Go語言之門:入門、實戰與進階
- Python期貨量化交易實戰
- 征服C指針(第2版)