官术网_书友最值得收藏!

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:

  1. 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.

  2. 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.

主站蜘蛛池模板: 庆安县| 都昌县| 清徐县| 来凤县| 阳江市| 进贤县| 宁海县| 靖边县| 祁门县| 沙雅县| 临海市| 思茅市| 广德县| 太白县| 容城县| 宜丰县| 曲麻莱县| 平顶山市| 聂拉木县| 临夏市| 濮阳市| 津南区| 五台县| 镇江市| 天门市| 利川市| 舞阳县| 焦作市| 临海市| 酒泉市| 察哈| 报价| 娄烦县| 南郑县| 黄龙县| 兴业县| 义马市| 曲周县| 务川| 赫章县| 柳江县|