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

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

主站蜘蛛池模板: 乐都县| 织金县| 霍邱县| 平舆县| 青岛市| 鄂伦春自治旗| 潜江市| 邻水| 南安市| 南宫市| 洱源县| 临邑县| 离岛区| 铜梁县| 蓬莱市| 荣成市| 麻栗坡县| 河曲县| 临沭县| 上饶县| 水城县| 耿马| 甘洛县| 卓尼县| 库伦旗| 辽中县| 苏尼特右旗| 夏邑县| 策勒县| 阳原县| 鹤庆县| 余庆县| 彝良县| 鹤峰县| 呼玛县| 营口市| 玉田县| 江达县| 柘城县| 固原市| 堆龙德庆县|