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

Recognizing touch shapes

There is a useful tool provided by Kivy that permits us to recognize the shape of the touch that is performed. In this recipe, we will go through the foundations of using it.

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. Also, this recipe will use the common button and label widgets for reference.

How to do it…

Follow these steps:

  1. First, in the KV file, define a button and an empty label:
    <MyW>:
        Button:
            id: button1
            pos: 0,0
            text: 'Hello'
    
        Label:
            id: label1
            pos: 50, 200
            text: ''
  2. In the class of the widget in the Python code, we need to override the method on_touch_move.
  3. Change the button position to the information in touch.pos.
  4. Change the text in the label when a rectangular shape is present:
    import kivy
    kivy.require('1.9.0') 
    
    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.input.shape import ShapeRect
    
    class MyW(Widget):
    
        def on_touch_move(self, touch):
            if 'pos' in touch.profile:
                self.ids.button1.pos = touch.pos
            if isinstance(touch.shape, ShapeRect):
                self.ids.label1.text =\
    'My touch have a rectangle shape of size' + str(touch.shape.width)+ str(touch.shape.height)
    
    class e6App(App):
    
        def build(self):
            return MyW()
    
    if __name__ == '__main__':
        e6App().run()

How it works…

In the KV file, the first line as usual is the name of the rule. 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. The sixth line is the definition of the label widget, the seventh line is ID of the label, the eighth line is the initial position of the label, and the ninth line is the initial text of the label.

In relation to the Python code, the initial four lines are the usual lines to use Kivy and the button widget. Next, the fifth one:

from kivy.input.shape import ShapeRect

In this line, we import the information of ShapeRect to be compared with the touch performed in the app. The sixth line is:

class MyW(Widget):

This is where we define the class associated with our rule in the KV file. The seventh 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, and this parameter is necessary to call the event by the input. The eighth and ninth lines are:

 if 'pos' in touch.profile:
 self.ids.button1.pos = touch.pos

Those lines change the position of the button. The tenth line is:

 if isinstance(touch.shape, ShapeRect):

It is where the comparison between the imported shape and the shape of the touch is performed. The eleventh line will change the label text with the dimension of the touch if this is rectangular. The last lines of the Python code are the usual lines to run and display our Kivy interface. Just remember that the initial part of the name of the class is:

class e6App(App):

It will be the same of the KV file; the name of the KV file in this case is e6.kv

See also

If you want to run your interface, take a look at our recipe Running your code, and to get more details about widgets, see the recipes in Chapter 4, Widgets.

主站蜘蛛池模板: 临泽县| 七台河市| 富民县| 蒙山县| 盈江县| 湘乡市| 莱州市| 塔城市| 进贤县| 策勒县| 全南县| 香河县| 津市市| 泽普县| 建阳市| 马关县| 泗阳县| 玉门市| 保德县| 澄迈县| 特克斯县| 闸北区| 黔江区| 手游| 衡阳县| 沅陵县| 理塘县| 临西县| 凤庆县| 万宁市| 海丰县| 习水县| 天全县| 台江县| 会理县| 定陶县| 阜阳市| 海宁市| 榆社县| 额敏县| 尼木县|