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 3486814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:04:31+00:00 2026-05-18T11:04:31+00:00

One question : How do you dynamically draw to a Tkinter Canvas object from

  • 0

One question: How do you dynamically draw to a Tkinter Canvas object from a button click?

This topic was posted originally by user339860 (see 1) as a two part question, but the second portion of the question has not yet been addressed. I’m experiencing the same problem, specifically I cannot draw to a Canvas object from a button event. Getting this question answered will help two people, myself and user339860; please take a look.

The application creates two frames positioned left and right, the second frame contains the Canvas object. I have a button in the first frame bound to a function/method named drawRectangle. The application runs fine, it even draws a rectangle in the Canvas object using the create_rectangle method, until you click the button. When you click the button you get a message with the following;

tkinter_app_27Nov2010.py", line 25, in drawRectangle
self.myCan.create_rectangle(64,64,110,110,fill=’blue’)
AttributeError: ‘NoneType’ object has no attribute ‘create_rectangle’

I thought it might have something to do with the scope of the Canvas object, so I created a class level variable set to None, but that didn’t fix the issue. I thought about the Canvas display list (see 2), but the Tk manual pages don’t reflect a method of adding a new object that I could find.

CODE:


# tkinter_app_27Nov2010.py
from Tkinter import *
class Application(Frame):
myCan = None
def createWidgets(self):
    uiFrame = Frame(self,width=300,height=30)
    uiFrame.configure(background='#e0e0e0')
    uiFrame.grid(row=0,column=0,sticky=N+S)

    outputFrame = Frame(self,width=300,height=300,background='#C0C0C0')
    outputFrame.grid(row=0,column=1)

    newBtn = Button(uiFrame,text="testing",command=self.drawRectangle)
    newBtn.grid(row=0,column=0)
    fillLbl = Label(uiFrame,text='-').grid(row=1,sticky=N+S)
    
    newLBL = Label(outputFrame,text="another testing",background='#C0C0C0')
    newLBL.grid(row=0)

    myCan = Canvas(outputFrame,width=300,height=300,borderwidth=1,relief='sunken')
    myCan.grid(row=1)
    myCan.create_rectangle(34,34,50,50,fill='red')

def drawRectangle(self):
    self.myCan.create_rectangle(64,64,110,110,fill='blue')
    
def __init__(self,master):
    Frame.__init__(self,master)
    self.pack()
    self.createWidgets()

root = Tk()
myApp = Application(master=root)
root.title("Tkinter Testing!")
myApp.mainloop()


There has to be a way to get a handle on the "damage/repair display model" (see 3) the Tkinter Canvas object uses to update itself. Please help!

References:

  1. stackoverflow.com/questions/2824041/dynamically-add-items-to-tkinter-canvas

  2. http://www.tcl.tk/man/tcl8.4/TkCmd/canvas.htm#M16

  3. effbot.org/tkinterbook/canvas.htm#performance-issues

  • 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-05-18T11:04:32+00:00Added an answer on May 18, 2026 at 11:04 am

    This is a Python problem, not a tkinter one. You have defined local variables inside createWidgets, but you haven’t set them as instance attributes. You need to use self.foo for that:

    >>> class Foo:
    ...     def __init__(self):
    ...             bar = "baz"
    ...
    >>> class Bar:
    ...     def __init__(self):
    ...             self.bar = "baz"
    ...
    >>> foo = Foo()
    >>> foo.bar
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: Foo instance has no attribute 'bar'
    >>> bar = Bar()
    >>> bar.bar
    'baz'
    

    Notice that you are indeed correct: the problem is to do with the scope of the Canvas. Or more precisely, with the scope of the myCan variable. If you hadn’t defined the class variable myCan, the lookup self.myCan would have raised a revealing AttributeError.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I got this question from one of the developers in my team: What
Surprisingly I was only able to find one previous question on SO about this
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
I recently asked this question: Dynamically Query a Database to check for value And
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I just finished a test as part of a job interview, and one question
I am looking into Cloud Computing for my company. The one question that I
I got one big question. I got a linq query to put it simply
After considering the answers to my previous question ( One SVN Repository or many?

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.