Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:04:18+00:00 2026-06-12T05:04:18+00:00

In this Kivy code : class MyPaintWidget(Widget): def on_touch_down(self, touch): userdata = touch.ud with

  • 0

In this Kivy code:

class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        userdata = touch.ud
        with self.canvas:
            Color(1, 1, 0)
            d = 30.
            Ellipse(pos=(touch.x - d/2, touch.y - d/2), size=(d, d))
            userdata['line'] = Line(points=(touch.x, touch.y))

Apparently Color and d and Ellipse are within the namespace of self.canvas, but how does Python know that userdata is not within that same namespace?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:04:19+00:00Added an answer on June 12, 2026 at 5:04 am

    Edit: This answer got a bit lengthy, so here’s the summary:

    1. with self.canvas defines the currently active canvas for the following code block.
    2. All drawing instructions like Color or Ellipse draw on the active canvas.

    Namespaces don’t really have anything to do with it, it’s the context that matters (see below).

    The with statement allows you to use so called context managers.

    The syntax is like this

    with thing [as foo]:
    

    where thing usually is a function decorated with the contextlib.contextmanager decorator. What exactly a context manager does depends on how thing is implemented.

    But what it doesn’t do is make variable magically appear in your scope. A reference to the context may be obtained by the optional as foo clause, but that’s it. Color and Ellipse in your example are coming from somewhere else (probably imports?).

    In order to find out what exactly the context manager in the with self.canvas line does, you should look at the API documentation or the source code for kivy.graphics.instructions.Canvas.

    Here’s the relevant excerpt from the tutorial:

    By using the with statement with it, all successive drawing commands
    that are properly indented will modify this canvas. The with statement
    also makes sure that after our drawing, internal state can be cleaned
    up properly.

    So the use of Color and Ellipse affects self.canvas, but they’re not defined in any way by the with statement.

    Looking at the source code, this is how it works:

    def class CanvasBase(InstructionGroup):
        def __enter__(self):
            pushActiveCanvas(self)
    
        def __exit__(self, *largs):
            popActiveCanvas()
    

    __enter__ and __exit__ define what happens if a context manager is entered (before the first line of indented code after the with statement) and exited.

    In this case, the canvas simply gets pushed onto a stack that defines the currently active canvas (and popped from it if the context manager is exited).

    In kivy.graphics.instructions.Instruction, the apparent base class for all drawing instructions, the parent is set to the currently active canvas:

    self.parent = getActiveCanvas()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is rejected by (at least) MSVC, ICC, and GCC: class A {
I'm having trouble changing the color of a simple widget in Kivy. I can
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
This code compiles and runs though gives a Microsoft compiler error that I cant
This is an example of the code am using. I use a modified version
This has been bugging me lately. Say I have a base class Base. If
What Kivy tutorials and learning aids exist? Where is the list for Kivy? This
According to this post: https://groups.google.com/forum/?fromgroups#!topic/kivy-users/n7c3thksnzg , it is possible to use Eclipse as an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.