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

  • Kivy Cookbook
  • Hugo Solis
  • 574字
  • 2021-07-16 20:39:57

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.

主站蜘蛛池模板: 玉门市| 永昌县| 周口市| 古田县| 雷山县| 秀山| 大渡口区| 莫力| 阿克| 南昌县| 延安市| 新兴县| 密云县| 通化县| 诏安县| 张家界市| 新安县| 禹城市| 天台县| 石嘴山市| 龙游县| 成安县| 新绛县| 会同县| 美姑县| 阿巴嘎旗| 开阳县| 吉木乃县| 黎平县| 纳雍县| 江川县| 阿巴嘎旗| 英吉沙县| 内丘县| 怀化市| 宣汉县| 和田县| 离岛区| 卢氏县| 兰西县| 乡城县|