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

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:

  1. First, in the KV file, define a button:
    <MyW>:
        Button:
            id: button1
            pos: 0,0
            text: 'Hello'
  2. In the class of the widget in the Python code, we need to override the method on_touch_move.
  3. 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.

主站蜘蛛池模板: 庆云县| 方山县| 寿阳县| 秦皇岛市| 双江| 北宁市| 离岛区| 盈江县| 安远县| 绥滨县| 武平县| 维西| 邹城市| 河间市| 陇南市| 阳信县| 和硕县| 高唐县| 南漳县| 班玛县| 康马县| 乐亭县| 平潭县| 宜昌市| 西青区| 绥芬河市| 白玉县| 永善县| 华池县| 海淀区| 孟连| 台东市| 通辽市| 来安县| 安泽县| 吐鲁番市| 金川县| 渭源县| 弥勒县| 麟游县| 招远市|