- Kivy Cookbook
- Hugo Solis
- 730字
- 2021-07-16 20:39:56
Using the mouse
This recipe will teach you how to use the first kind of input, probably the most used, the mouse. We will consider the mouse input as a touch event, so the (x, y) position of the touch (0-1 range) will be scaled to the window size (width/height) and dispatched to the following:
- on_touch_down: An event is fired when a touch down event is initiated. Subsequently, the respective widget's
on_touch_down()
method is called. - on_touch_move: An event is fired when a touch event moves (changes location). Subsequently, the respective widget's
on_touch_move()
method is called. - on_touch_up: An event is fired when a down event is released (terminated). Subsequently, the respective widget's
on_touch_up()
method is called.
Getting ready
In this recipe, we will use the Kv language for the design of the widgets, so you will need to be familiar with the language or have completed the previous chapter. Also, this recipe will use the common Button
widget for reference.
How to do it…
Follow the steps in this recipe:
- First, let's start with the KV file:
<MyW>: Button: id: button1 text: 'Hello'
In the class of the widget in the Python code, we need to override the method
on_touch_down
because the method is already defined in the Kivy framework. - Change the button text with the information in
touch.button
:import kivy kivy.require('1.9.0') from kivy.app import App from kivy.uix.widget import Widget class MyW(Widget): def on_touch_down(self, touch): if 'button' in touch.profile: self.ids.button1.text = touch.button class e1App(App): def build(self): return MyW() if __name__ == '__main__': e1App().run()
How it works…
Let's start with the KV file. The first line is the name of the rule, similar to the class name in the Python file. The second line is the definition of the button widget, and the third line is the ID of the button, which is necessary to do the call of the widget inside the Python code, and the fourth line is the definition of the text in the button.
With regard to the Python code, the initial four lines are usual to use Kivy and the button widget. Note the following fifth line:
class MyW(Widget):
This is where we define the class associated with our rule in the KV file. Now consider the sixth line:
def on_touch_down(self, touch):
Here, we start the declaration of the dispatched on_touch_down
method; you must note the parameter touch in the declaration, and this parameter is necessary to call the event using the input—in this case—the mouse. Next, the seventh line is:
if 'button' 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 line is performed, the app could crash. Finally, the eighth line:
self.ids.button1.text = touch.button
This line is where we do the call to the button profile, which gives the information on which mouse button is touched by the user (the right one, left one, scroll up, and so on). Using this information, we change the text in our button that has ID button1.
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 classis as follows:
class e1App(App):
Note that it will be the same as the KV file, so the name of the KV file in this case is e1.kv
.
A last thing to remark is that we do not limit the input event to the button, so the touch can occur anywhere within the window.
There's more…
Another interesting profile that we could call for the mouse is the pos
attribute, which provides us with the position vector of the click in the window. Let's change the seventh line of the Python code to:
self.ids.button1.text = str(touch.pos)
When we are calling the pos
profile and changing the text in the button widget to the position where the click is performed, the str()
built-in object is important because the touch.pos
return is a vector. This means that we need to convert to a string to avoid compatibility issues.
See also
If you want to run your interface, take a look at our recipe Running your code. To get further details about widgets, see the recipes in Chapter 4, Widgets.
- Spring 5.0 Microservices(Second Edition)
- 軟件測試項目實戰之性能測試篇
- C語言程序設計
- 深度學習:算法入門與Keras編程實踐
- Full-Stack React Projects
- Linux:Embedded Development
- Raspberry Pi Robotic Blueprints
- Elasticsearch Essentials
- Python語言科研繪圖與學術圖表繪制從入門到精通
- 深入實踐DDD:以DSL驅動復雜軟件開發
- OpenCV 3.0 Computer Vision with Java
- HTML5游戲開發實戰
- Drupal Search Engine Optimization
- Java EE輕量級解決方案:S2SH
- Mastering JavaScript Promises