- 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.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- Instant Apache Stanbol
- HTML5 移動Web開發從入門到精通(微課精編版)
- OpenCV 3和Qt5計算機視覺應用開發
- Java:Data Science Made Easy
- Python GUI Programming Cookbook
- Hands-On RESTful Web Services with Go
- Visual C
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- 小程序從0到1:微信全棧工程師一本通
- Python Projects for Kids
- ANSYS FLUENT 16.0超級學習手冊
- Python滲透測試編程技術:方法與實踐(第2版)
- Mastering Unity 2017 Game Development with C#(Second Edition)
- C語言進階:重點、難點與疑點解析