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

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.

主站蜘蛛池模板: 太保市| 怀宁县| 牡丹江市| 仁布县| 南汇区| 嘉禾县| 花莲县| 屯留县| 西平县| 哈密市| 五寨县| 沛县| 瓦房店市| 南雄市| 靖远县| 泰安市| 綦江县| 贡嘎县| 镇安县| 都江堰市| 勐海县| 湘阴县| 仙游县| 富裕县| 茌平县| 延庆县| 新竹县| 兴仁县| 额敏县| 苏尼特左旗| 阿尔山市| 华容县| 巴楚县| 柳州市| 沙洋县| 塘沽区| 宜城市| 静宁县| 化隆| 政和县| 广南县|