Reputation: 21
I'm a newbie programmer and am currently learning python with kivy as a GUI platform.
I am reading the kivy manual and was working on widgets. I wanted to try some stuff on the tutorial painter widget, but after hours of trying, failed to do so.
What I want should be quite simple. I have the widget where it creates a random line after touching the screen. I thought it would be fun to automatically add lines repeatedly after touching the screen at a certain area. So I made a function that keeps "injecting" the widget with data to create more lines.
But I simply can not "communicate" with the widget. I have no idea what the widget "instance" name is. So I created the instance by naming it painter, well let's just share the code:
import kivy
import time
from random import random
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line
class MyPaintWidget(Widget):
def on_touch_down(self, touch,):
with self.canvas:
Color(random(), random(), random())
touch.ud['Line'] = Line(points=(touch.x, touch.y))
touch.ud['Line'].points += [random()*1000, random()*1000 ]
begin()
def touchme():
touch.ud['Line'].points += [random()*1000, random()*1000 ]
print 'touchme'
class MyPaintApp(App):
def build(self):
painter = MyPaintWidget()
return painter
def begin():
def my_callback(dt):
print 'begin'
painter.touchme()
Clock.schedule_interval(my_callback, 1.)
if __name__ == '__main__':
MyPaintApp().run()
Hopefully soneone can provide me with an answer of how to do that and maybe explain to me a bit how the widgets work. I'm treating it as a standard class in python, but I think it works a little different than that.
Cheers.
Upvotes: 2
Views: 1909
Reputation: 29953
Not sure if I understand what you're trying to achieve, but I guess you could just move the code from your begin
method into the on_touch_down
method, to make the latter look like this:
def on_touch_down(self, touch):
with self.canvas:
Color(random(), random(), random())
touch.ud['Line'] = Line(points=(touch.x, touch.y))
touch.ud['Line'].points += [random()*1000, random()*1000 ]
Clock.schedule_interval(self.touchme, 1.)
The problem is that touchme
needs the touch
object, so you'd have to pass it as a parameter. Sth like this might work, but I didn't check:
def on_touch_down(self, touch):
with self.canvas:
Color(random(), random(), random())
touch.ud['Line'] = Line(points=(touch.x, touch.y))
touch.ud['Line'].points += [random()*1000, random()*1000 ]
Clock.schedule_interval(lambda: self.touchme(touch), 1.)
def touchme(self, touch):
touch.ud['Line'].points += [random()*1000, random()*1000 ]
print 'touchme'
Generally I'd recommend you try to get a better feeling for variable scopes. In your code you try to access the variables painter
and touch
where they just aren't visible.
I'm only getting started with kivy myself, so maybe I can give a more complete answer in a few weeks. ;)
Upvotes: 5