The distinction between a class and instance is a little confusing. You can think of the class a recipe for an object, and the instance is the object.
You created an instance of the DrawArea and saved it to self.painter, you needed to use the instance to call the draw_rectangle method.
Here are some resources I hope you will find helpful:
https://realpython.com/python3-object-oriented-programming/
The code example below will run, and addresses your issue.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.core.window import Window
from kivy.graphics import Rectangle
class DrawArea
(Widget):
def draw_rectangle(self): # change name CamelCase for classes, snake_case for methods. See pep8
with self.canvas:
Color(1, 0, 0, 0.5, mode="rgba")
Rectangle(pos=(100, 100), size=(100, 50))
class DrawApp(App):
def build(self):
self.title = "TestApp"
Window.size = (400, 300)
Window.clearcolor = (102 / 255, 102 / 255, 102 / 255, 1)
parent = Widget()
self
.painter = DrawArea()
self.draw_button = Button( # name change
text="Draw",
font_size=14,
pos=(10, 10),
size_hint=(None, None),
size=(70, 30),
color=(64, 64, 64, 1)
)
self.draw_button.bind(on_release=self.draw_something)
disable_button = Button(
text="Disable Draw Button",
font_size=14,
pos=(150, 10),
size_hint=(None, None),
size=(150, 30),
color=(64, 64, 64, 1)
)
disable_button.bind(on_press=self.call_back)
parent.add_widget(self.painter)
parent.add_widget(self.draw_button)
parent.add_widget(disable_button)
return parent
def draw_something(self, obj):
self.painter.draw_rectangle()
def call_back(self, elem):
self.draw_button.disabled = True
if __name__ == '__main__':
DrawApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a8308e42-12fd-46b6-8c4c-7765b68c5a49o%40googlegroups.com.
One additional change… You want to add widgets to Layouts, not other widgets.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.core.window import Window
from kivy.graphics import Rectangle
from kivy.uix.floatlayout import FloatLayout
class DrawArea(Widget):
def draw_rectangle(self): # change name CamelCase for classes, snake_case for methods. See pep8
with self.canvas:
Color(1, 0, 0, 0.5, mode="rgba")
Rectangle(pos=(100, 100), size=(100, 50))
class DrawApp(App):
def build(self):
self.title = "TestApp"
Window.size = (400, 300)
Window.clearcolor = (102 / 255, 102 / 255, 102 / 255, 1
)
parent = FloatLayout()
self.painter = DrawArea()
self.draw_button = Button( # name change
text="Draw",
font_size=14,
pos=(10, 10),
size_hint=(None, None),
size=(70, 30),
color=(64, 64, 64, 1)
)
self.draw_button.bind(on_release=self.draw_something)
disable_button = Button(
text="Disable Draw Button",
font_size=14,
pos=(150, 10),
size_hint=(None, None),
size=(150, 30),
color=(64, 64, 64, 1)
)
disable_button.bind(on_press=self.call_back)
parent.add_widget(self.painter)
parent.add_widget(self.draw_button)
parent.add_widget(disable_button)
return parent
def draw_something(self, obj):
self.painter.draw_rectangle()
def call_back(self, elem):
self.draw_button.disabled = True
if __name__ == '__main__':
DrawApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5ff22643.1c69fb81.a617a.82a5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a8308e42-12fd-46b6-8c4c-7765b68c5a49o%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
The disable_button does not need to be an instance variable self.disable_button, because it is only used in the local scope of build().
The instance variable self.painter is initialized and created in build() and also using in draw_something(). It is used in multiple methods in the class. It is part of the classes state. Same with self.draw_button. As you add more capabilities you will likely need to make disable_button an instance variable.
It is customary in python to declare all of a classes instance variables in the classes __init__() method.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c1922e89-775d-4796-9d15-ff2c5b78e7d8o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c1922e89-775d-4796-9d15-ff2c5b78e7d8o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/dfa8b94f-9d65-4417-86e7-fe922451a69ao%40googlegroups.com.
def _on_key_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'up':
with self.canvas:
Color(1,0,0,0.5, mode="rgba")
Rectangle(pos=(100,100), size=(100,50))
if keycode[1] == 'down':
app = App.get_running_app()
app.disable_next_button()
You need to get the object, the method you want disable_next_button() is a method of App. The instance of app is App.get_running_app()
Read: https://kivy.org/doc/stable/api-kivy.app.html#kivy.app.App.get_running_app
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/dfa8b94f-9d65-4417-86e7-fe922451a69ao%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/dfa8b94f-9d65-4417-86e7-fe922451a69ao%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/dfa8b94f-9d65-4417-86e7-fe922451a69ao%40googlegroups.com.