Leyendo ..kv archivo en la aplicación kivy
-
20-12-2019 - |
Pregunta
Tengo un archivo main.py con el siguiente código:
from kivy.app import App
from kivy.config import Config
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.lang import Builder
from enum import IntEnum
from enum import Enum
from random import random
class Vertice(Widget):
def __init__(self, pos=None):
self.pos = pos
#print self.pos
print self.size
class Type(IntEnum):
RIGHT_TOP = 1
RIGHT_BOTTOM = 2
LEFT_BOTTOM = 3
LEFT_TOP = 4
UNDEFINED = 5
class Direction(Enum):
CLOCKWISE = 1
COUNTER_CLOCKWISE = 2
UNDEFINED = 3
class MindMapWidget(Widget):
circleShape = [Type.RIGHT_TOP, Type.RIGHT_BOTTOM, Type.LEFT_BOTTOM, Type.LEFT_TOP]
editing = False
bounds = []
vertices = []
def getType(self, dx, dy, direction):
result = Type.UNDEFINED
if direction != Direction.UNDEFINED:
if dx > 0 and dy > 0:
if (self.isClockwise(direction)):
result = Type.RIGHT_TOP
else:
result = Type.LEFT_BOTTOM
else:
if dx < 0 and dy > 0:
if (self.isClockwise(direction)):
result = Type.RIGHT_BOTTOM
else:
result = Type.LEFT_TOP
else:
if dx < 0 and dy < 0:
if (self.isClockwise(direction)):
result = Type.LEFT_BOTTOM
else:
result = Type.RIGHT_TOP
else:
if dx > 0 and dy < 0:
if (self.isClockwise(direction)):
result = Type.LEFT_TOP
else:
result = Type.RIGHT_BOTTOM
print result
return result
def getPointsXAndY(self, points, index):
startIndex = index * 2
return [points[startIndex], points[startIndex + 1]]
def isClockwise(self, direction):
return direction == Direction.CLOCKWISE
def isCircle(self, points, direction):
result = False
shape = self.circleShape
STEP = 5
index = 0
startIndex = 0
detected = [None] * 4
currentPoint = self.getPointsXAndY(points, 0)
currentType = None
if direction != Direction.UNDEFINED:
for x in range(STEP, len(points), STEP):
try:
nextPoint = self.getPointsXAndY(points, x)
except:
break
dx = nextPoint[0] - currentPoint[0]
dy = -(nextPoint[1] - currentPoint[1])
if dx == 0 or dy == 0:
continue
newType = self.getType(dx, dy, direction)
# get the first item, or if the direction has changed
if currentType is None or currentType != newType:
# if we have at least the first direction, check if the next direction is the next step in the circle drawing process
if currentType is not None:
if newType != shape[index]:
break
# a circle is a distionct set of drawing directions, so we need to follow them in order, from where the user started drawing
else:
index = shape.index(newType)
startIndex = index
self.bounds.insert(index, currentPoint)
# don't double up on actions, because we need to compare lists
if detected.count(newType) == 0:
print index
#detected.insert(index, newType)
detected[index] = newType
# because we may start anywhere in the shape, we may need to do circular traversal of the shape array
if direction == Direction.CLOCKWISE:
index = (index + 1) % len(shape)
else:
index = (index - 1) % len(shape)
currentType = newType
currentPoint = nextPoint
if shape == detected:
result = True
break
return result
"""def reverseDirection(self, direction):
result = None
if direction == Direction.UNDEFINED:
result = direction
else:
if direction == Direction.CLOCKWISE:
result = Direction.COUNTER_CLOCKWISE
else:
result = Direction.CLOCKWISE
return result"""
def determineDirection(self, points):
result = Direction.UNDEFINED
total = 0
currentPoint = None
previousPoint = None
for i in range(0, len(points)/2, 1):
currentPoint = self.getPointsXAndY(points, i)
if previousPoint is not None:
total += (currentPoint[0] - previousPoint[0]) * (currentPoint[1] + previousPoint[1])
previousPoint = currentPoint
if total > 0:
result = Direction.CLOCKWISE
else:
if total < 0:
result = Direction.COUNTER_CLOCKWISE
return result
def on_touch_down(self, touch):
editing = True
color = (random(), 1., 1.)
with self.canvas:
Color(*color, mode='hsv')
touch.ud['line'] = Line(points=(touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
def on_touch_up(self, touch):
editing = False
direction = None
if len(touch.ud['line'].points) > 0:
points = touch.ud['line'].points
if self.isCircle(points, self.determineDirection(points)):
print "circle"
with self.canvas:
#Ellipse(pos=(self.bounds[0]), size=(50, 50))
Vertice(pos=self.bounds[0])
else:
print "unknown"
class MindMapApp(App):
def build(self):
parent = Widget()
painter = MindMapWidget()
clearbtn = Button(text='Clear')
parent.add_widget(painter)
parent.add_widget(clearbtn)
def clear_canvas(obj):
painter.canvas.clear()
clearbtn.bind(on_release=clear_canvas)
Config.set('input','multitouchscreen1','tuio,192.168.0.4:3333')
return parent
if __name__ == '__main__':
MindMapApp().run()
Tengo un archivo MINDMAP.KV:
#:kivy 1.8
<Vertice>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
La salida de la declaración:
print self.size
es:
[100,100]
que es el tamaño predeterminado del widget.Cuando la aplicación se carga, tira esta depuración:
[DEBUG ] [App ] Loading kv <./mindmap.kv>
Esto me dice que el archivo .kv se está cargando y no se está interpretado correctamente.¿Me estoy perdiendo un enlace?No estoy seguro de cómo solucionar este problema.
Solución
MindMap.kv debe ser MINMAPAP.KV I.E. Debe estar en letras pequeñas.Por favor intente.
También para ejecutar esto, debe agregar debajo de la línea al final de su código
if __name__ == '__main__':
MindMapApp().run()
Otra forma de cargar. El archivokv está usando el constructor Builder.load_File ('ruta / a / file.kv')
Para obtener más información, comprobar http://kivy.org/docs/guide/lang.html
Cambie su clase de verticio a seguir:
class Vertice(Widget):
def __init__(self, pos=None):
super(Vertice, self).__init__()
self.pos = pos
#print self.pos
print self.size