- Kivy Cookbook
- Hugo Solis
- 817字
- 2021-07-16 20:39:56
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:
- 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'
- In the class of the widget in the Python code, we need to override the method
on_touch_down
- Change the button text with the information in
touch.button
- 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.
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- The React Workshop
- Python數據結構與算法(視頻教學版)
- JavaCAPS基礎、應用與案例
- ExtJS高級程序設計
- Mastering Data Mining with Python:Find patterns hidden in your data
- Learning PHP 7
- 打開Go語言之門:入門、實戰與進階
- 編寫高質量代碼:改善Objective-C程序的61個建議
- 關系數據庫與SQL Server 2012(第3版)
- Python趣味創意編程
- 虛擬現實:引領未來的人機交互革命
- C#程序開發參考手冊
- 面向對象分析與設計(第3版)
- C語言從入門到精通(第4版)