Tutorial Kivy Python : Pemrograman Kivy untuk Touchtracer

Tutorial Kivy Python : Pemrograman Kivy untuk Touch tracer - Berikut ini adalah contoh pemrograman Touchtracer dari Kivy.org . Touchtracer akan memperlihatkan position dari kursor koordinat didalam window touchtracer ketika kita menekan mouse dan akan muncul X axis dan Y axis dari koordinat yang kita gerakan dan akan muncul pula garis sesuai dengan gerakan mouse kita didalam Windows Touchtracer. 

Ada 2 File  Program yaitu Touchtracer Main.py dan Touchtracer.kv . Untuk program python bisa di save di IDLE Python 2.7.18 sedangkan untuk program .kv di save menggunakan notepad++. Silahkan download notepad++ disini. 
Listing File Touchtracer Main.py  :
 
'''
Touch Tracer Line Drawing Demonstration
=======================================
Progam Ini menunjukkan pelacakan setiap sentuhan yang terlist ke perangkat. Anda harus
melihat gambar latar belakang dasar. Saat Anda menekan dan menahan mouse, Anda
harus melihat tanda silang dengan koordinat tertulis di sebelahnya. Ketika
Anda drag, akan meninggalkan jejak. Informasi tambahan, seperti tekanan,
akan ditampilkan jika ada di profil sentuh perangkat Anda.
.. catatan::
Sebuah fungsi `calculate_points` menangani titik-titik yang akan digambar
telah secara default menerapkan penundaan 5 langkah. Untuk mendapatkan visual yang lebih presisi
hasil menurunkan nilai argumen kata kunci opsional `langkah`.
Program ini menetapkan sebuah ikon, file icon.png, dalam subkelas App-nya.
Itu juga menggunakan file particle.png sebagai sumber untuk menggambar jalur yang
berwarna putih pada transparan. File touchtracer.kv menjelaskan aplikasi.
File android.txt digunakan untuk mengemas aplikasi untuk digunakan dengan
Aplikasi Android Kivy Launcher. Untuk perangkat Android, Anda dapat
salin/tempel direktori ini ke /sdcard/kivy/touchtracer di perangkat Android Anda.

'''
__version__ = '1.0'
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Color, Rectangle, Point, GraphicException
from random import random
from math import sqrt
def calculate_points(x1, y1, x2, y2, steps=5):
dx = x2 - x1
dy = y2 - y1
dist = sqrt(dx * dx + dy * dy)
if dist < steps:
return
o = []
m = dist / steps
for i in range(1, int(m)):
mi = i / m
lastx = x1 + dx * mi
lasty = y1 + dy * mi
o.extend([lastx, lasty])
return o
class Touchtracer(FloatLayout):
def on_touch_down(self, touch):
win = self.get_parent_window()
ud = touch.ud
ud['group'] = g = str(touch.uid)
pointsize = 5
if 'pressure' in touch.profile:
ud['pressure'] = touch.pressure
pointsize = (touch.pressure * 100000) ** 2
ud['color'] = random()
with self.canvas:
Color(ud['color'], 1, 1, mode='hsv', group=g)
ud['lines'] = [
Rectangle(pos=(touch.x, 0), size=(1, win.height), group=g),
Rectangle(pos=(0, touch.y), size=(win.width, 1), group=g),
Point(points=(touch.x, touch.y), source='particle.png',
pointsize=pointsize, group=g)]
ud['label'] = Label(size_hint=(None, None))
self.update_touch_label(ud['label'], touch)
self.add_widget(ud['label'])
touch.grab(self)
return True
def on_touch_move(self, touch):
if touch.grab_current is not self:
return
ud = touch.ud
ud['lines'][0].pos = touch.x, 0
ud['lines'][1].pos = 0, touch.y
index = -1
while True:
try:
points = ud['lines'][index].points
oldx, oldy = points[-2], points[-1]
break
except:
index -= 1
points = calculate_points(oldx, oldy, touch.x, touch.y)
# if pressure changed create a new point instruction
if 'pressure' in ud:
if not .95 < (touch.pressure / ud['pressure']) < 1.05:
g = ud['group']
pointsize = (touch.pressure * 100000) ** 2
with self.canvas:
Color(ud['color'], 1, 1, mode='hsv', group=g)
ud['lines'].append(
Point(points=(), source='particle.png',
pointsize=pointsize, group=g))
if points:
try:
lp = ud['lines'][-1].add_point
for idx in range(0, len(points), 2):
lp(points[idx], points[idx + 1])
except GraphicException:
pass
ud['label'].pos = touch.pos
import time
t = int(time.time())
if t not in ud:
ud[t] = 1
else:
ud[t] += 1
self.update_touch_label(ud['label'], touch)
def on_touch_up(self, touch):
if touch.grab_current is not self:
return
touch.ungrab(self)
ud = touch.ud
self.canvas.remove_group(ud['group'])
self.remove_widget(ud['label'])
def update_touch_label(self, label, touch):
label.text = 'ID: %s\nPos: (%d, %d)\nClass: %s' % (
touch.id, touch.x, touch.y, touch.__class__.__name__)
label.texture_update()
label.pos = touch.pos
label.size = label.texture_size[0] + 20, label.texture_size[1] + 20
class TouchtracerApp(App):
title = 'Touchtracer'
icon = 'icon.png'
def build(self):
return Touchtracer()
def on_pause(self):
return True
if __name__ == '__main__':
TouchtracerApp().run()
 
Listing Program Toutchtracer.kv  :
 
#:kivy 1.0
#:import kivy kivy
<Touchtracer>:
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
source: 'data/images/background.jpg'
size: self.size
BoxLayout:
padding: '10dp'
spacing: '10dp'
size_hint: 1, None
pos_hint: {'top': 1}
height: '44dp'
Image:
size_hint: None, None
size: '24dp', '24dp'
source: 'data/logo/kivy-icon-64.png'
mipmap: True
Label:
height: '24dp'
text_size: self.width, None
color: (1, 1, 1, .8)
text: 'Kivy %s - Touchtracer' % kivy.__version__
valign: 'middle'
 
File Android.txt dan file icon.png 
 
Listing File Android.txt :
title=Touchtracer
author=Kivy team
orientation=landscape
 
Hasil dari program Touchtracer , sebelum mouse di tekan :
 
 Ketika mouse di tekan dan digerakan, terlihat program menuliskan koordinat mouse 
didalam window touchtracer dan garis terbentuk mengikuti pola gerakan mouse.
 
Tampilan lainnya dari Touchtracer :


Subscribe to receive free email updates:

0 Response to "Tutorial Kivy Python : Pemrograman Kivy untuk Touchtracer "

Post a Comment