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

  • 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:

  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.

主站蜘蛛池模板: 寿阳县| 会理县| 武义县| 沙坪坝区| 蒙阴县| 库尔勒市| 石阡县| 周至县| 邵阳县| 三亚市| 韶山市| 琼海市| 宁南县| 甘南县| 深圳市| 铁力市| 长乐市| 都江堰市| 新田县| 抚顺县| 盐边县| 东兰县| 彰武县| 炉霍县| 华容县| 高陵县| 酉阳| 宜阳县| 怀化市| 沾化县| 海盐县| 调兵山市| 酉阳| 于都县| 闽清县| 新乐市| 乐安县| 内江市| 固镇县| 齐河县| 南澳县|