AttributeError: 'kivy.graphics.context_instructions.Color' object has no attribute 'fbind' when I run my kivy code
Question:
What is wrong with the following code?
# Program to Show how to create a switch
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')
# Builder is used when .kv file is
# to be used in .py file
from kivy.lang import Builder
# The screen manager is a widget
# dedicated to managing multiple screens for your application.
from kivy.uix.screenmanager import ScreenManager, Screen
# You can create your kv code in the Python file
Builder.load_string("""
<ScreenOne>:
BoxLayout:
canvas.before:
Color:
rgba: (.4, .6, 9, 1) #(0,0,0,1)
Rectangle:
size: self.size
pos: self.pos
canvas:
Color:
rgba: (1, 1, 1,1)
RoundedRectangle:
size : self.width/2, self.height/3
pos : self.center_x - self.w
idth/4 , self.center_y - self.height/6
radius: [(40, 40), (40, 40), (40, 40), (40, 40)]
Button:
text: "Go to Screen 2"
background_color : 0, 0, 1, 1
on_press:
# You can define the duration of the change
# and the direction of the slide
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_two'
<ScreenTwo>:
BoxLayout:
Button:
text: "Go to Screen 3"
background_color : 1, 1, 0, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_three'
<ScreenThree>:
BoxLayout:
Button:
text: "Go to Screen 4"
background_color : 1, 0, 1, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_four'
<ScreenFour>:
BoxLayout:
Button:
text: "Go to Screen 5"
background_color : 0, 1, 1, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_five'
<ScreenFive>:
BoxLayout:
Button:
text: "Go to Screen 1"
background_color : 1, 0, 0, 1
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'screen_one'
""")
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenThree(Screen):
pass
class ScreenFour(Screen):
pass
class ScreenFive(Screen):
pass
# The ScreenManager controls moving between screens
screen_manager = ScreenManager()
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
screen_manager.add_widget(ScreenThree(name="screen_three"))
screen_manager.add_widget(ScreenFour(name="screen_four"))
screen_manager.add_widget(ScreenFive(name="screen_five"))
# Create the App class
class ScreenApp(App):
def build(self):
return screen_manager
# run the app
sample_app = ScreenApp()
sample_app.run()
I am new to kivy and got this code online which works perfect. I now tried to alter it by putting some canvas and shapes in it but I get the error:
AttributeError: 'kivy.graphics.context_instructions.Color' object has no attribute 'fbind'
what does this mean and how can I alter this again to achieve my goal of having my 2 rectangles in place and the button on them?
Answers:
Assuming you copied and pasted your code correctly, the indentation is wrong, you’ve put a Color instruction at child widget level rather than indented under the canvas.before:
. This is quite possibly causing your error.
What is wrong with the following code?
# Program to Show how to create a switch
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require('1.9.0')
# Builder is used when .kv file is
# to be used in .py file
from kivy.lang import Builder
# The screen manager is a widget
# dedicated to managing multiple screens for your application.
from kivy.uix.screenmanager import ScreenManager, Screen
# You can create your kv code in the Python file
Builder.load_string("""
<ScreenOne>:
BoxLayout:
canvas.before:
Color:
rgba: (.4, .6, 9, 1) #(0,0,0,1)
Rectangle:
size: self.size
pos: self.pos
canvas:
Color:
rgba: (1, 1, 1,1)
RoundedRectangle:
size : self.width/2, self.height/3
pos : self.center_x - self.w
idth/4 , self.center_y - self.height/6
radius: [(40, 40), (40, 40), (40, 40), (40, 40)]
Button:
text: "Go to Screen 2"
background_color : 0, 0, 1, 1
on_press:
# You can define the duration of the change
# and the direction of the slide
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_two'
<ScreenTwo>:
BoxLayout:
Button:
text: "Go to Screen 3"
background_color : 1, 1, 0, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_three'
<ScreenThree>:
BoxLayout:
Button:
text: "Go to Screen 4"
background_color : 1, 0, 1, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_four'
<ScreenFour>:
BoxLayout:
Button:
text: "Go to Screen 5"
background_color : 0, 1, 1, 1
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'screen_five'
<ScreenFive>:
BoxLayout:
Button:
text: "Go to Screen 1"
background_color : 1, 0, 0, 1
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'screen_one'
""")
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenThree(Screen):
pass
class ScreenFour(Screen):
pass
class ScreenFive(Screen):
pass
# The ScreenManager controls moving between screens
screen_manager = ScreenManager()
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
screen_manager.add_widget(ScreenThree(name="screen_three"))
screen_manager.add_widget(ScreenFour(name="screen_four"))
screen_manager.add_widget(ScreenFive(name="screen_five"))
# Create the App class
class ScreenApp(App):
def build(self):
return screen_manager
# run the app
sample_app = ScreenApp()
sample_app.run()
I am new to kivy and got this code online which works perfect. I now tried to alter it by putting some canvas and shapes in it but I get the error:
AttributeError: 'kivy.graphics.context_instructions.Color' object has no attribute 'fbind'
what does this mean and how can I alter this again to achieve my goal of having my 2 rectangles in place and the button on them?
Assuming you copied and pasted your code correctly, the indentation is wrong, you’ve put a Color instruction at child widget level rather than indented under the canvas.before:
. This is quite possibly causing your error.