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

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

主站蜘蛛池模板: 含山县| 新竹市| 和田市| 黔西县| 托克托县| 衡阳县| 甘德县| 榕江县| 城固县| 佛教| 太仆寺旗| 班玛县| 萍乡市| 普洱| 宁都县| 资中县| 汕头市| 修武县| 永济市| 望城县| 唐山市| 尉犁县| 枣强县| 深州市| 积石山| 万全县| 科技| 娄底市| 桑日县| 陇西县| 和田县| 神池县| 贵州省| 太谷县| 出国| 洛川县| 水富县| 茂名市| 万盛区| 富顺县| 永川市|