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

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.

主站蜘蛛池模板: 利辛县| 平阳县| 谷城县| 广宗县| 乌鲁木齐市| 宁城县| 太原市| 盱眙县| 静乐县| 景宁| 古浪县| 津市市| 拜泉县| 轮台县| 桂林市| 前郭尔| 黄山市| 旬阳县| 苍南县| 浪卡子县| 夹江县| 买车| 汉寿县| 丁青县| 晋州市| 六安市| 醴陵市| 天祝| 苏州市| 富蕴县| 台南县| 通许县| 达尔| 博客| 高邑县| 绥江县| 泸西县| 河源市| 平舆县| 淄博市| 扬中市|