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

Evolving to the touchscreen

In this recipe, we are evolving to the touchscreen. Here, you will see the basic differences between the mouse and the touchscreen. This will give us more options than with the mouse device.

Getting ready

For this recipe, we will use the Kv language for the design of the widgets, so make sure that you are confident with it and refresh your knowledge if necessary. Also, this recipe will use the common button and label widgets for reference. Obviously, to get more benefit, a touchscreen device is useful to run the code.

How to do it…

Perform the following steps:

  1. In the KV file, declare the button and the label:
    <MyW>:
        Button:
            id: button1
            text: 'Hello'
        Label:
            id: label1
            pos: 100, 100
            text: 'My label before press the screen'
  2. In the class of the widget in the Python code, we need to override the method on_touch_down
  3. Change the button text with the information in touch.button
  4. Change the label text with the information in touch.pressure
    import kivy
    kivy.require('1.9.0') 
    
    from kivy.app import App
    from kivy.uix.widget import Widget
    
    class MyW(Widget):
    
        def on_touch_down(self, touch):
            if 'button' in touch.profile:
                self.ids.button1.text = touch.button 
    
            if 'pressure' in touch.profile:
                self.ids.label1.text =\
                str(touch.pressure)
    
    class e2App(App):
    
        def build(self):
            return MyW()
    
    if __name__ == '__main__':
        e2App().run()

How it works…

Well, let's first review the KV file. The first line is the name of the rule, the same as of 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, and the fourth line is the definition of the text in the button. The fifth line is the definition of the label widget. The sixth line is where we give the ID to the label, the seventh line is where we give the position to the label. I should point out that the button is using the default position (0, 0), so we will have to give a different position to the label widget to avoid overlapping. The eight is the definition of the initial text in the label.

With regard to the Python code, the initial four lines are usual to use Kivy and the widgets. Note the fifth one:

class MyW(Widget):

This is where we define the class associated with our rule in the KV file. The sixth line:

def on_touch_down(self, touch):

Here, we start the declaration of the dispatched on_touch_down method. You must note the parameter touch in the declaration as this parameter is necessary to call the event, which is done by the input (in this case, the mouse). The seventh line is:

 if 'button' in touch.profile:

It 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 is:

 self.ids.button1.text = touch.button

This line is where we do the call to the profile button, which gives the information of what button is touched by the user (the right, left, scroll up button, and so on) and we changed the text in the button with the ID button1 with that information. The ninth line is:

if 'pressure' in touch.profile:

This is the other verification line and is important because, due to the fact that pressure is specific to some touchscreen devices, it is necessary to avoid crashes. The tenth and eleventh line:

self.ids.label1.text =\
str(touch.pressure)

Those lines are where we change the text of the label to the pressure value of the specific touch.

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 e2App(App):

It will be the same in relation to the KV file. For example, the name of the KV file in this case is e2.kv.

The final thing to remark is that we do not limit the input event to the button, so the touch can occur anywhere within the window or the screen.

There's more…

Actually, your device could have more profiles than we saw in this recipe. Let's change the code to know that for your specific device. Remove the ninth line and change the tenth and eleventh lines to:

self.ids.label1.text =\
str(touch.profile)

These lines will change the label's text for all the profiles available for your device. Thus, you can get more yield of your app.

See also

If you want to run your interface, take a look at our recipe Running your code, and to get more detail about widgets, see the recipes in Chapter 4, Widgets. If you want to run the code in a mobile device, see Chapter 9, Kivy for Mobile Devices.

主站蜘蛛池模板: 濮阳市| 大厂| 衡水市| 弥渡县| 泌阳县| 吴旗县| 汾西县| 定边县| 临高县| 双牌县| 贵定县| 江达县| 台东县| 玉溪市| 秦皇岛市| 二手房| 北碚区| 渑池县| 垣曲县| 湘潭市| 阿合奇县| 姚安县| 沈阳市| 新丰县| 洛隆县| 溧水县| 黄冈市| 南郑县| 昆明市| 景东| 大竹县| 铜梁县| 简阳市| 奉节县| 安溪县| 英山县| 龙井市| 盘锦市| 游戏| 宜君县| 开阳县|