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:
stackoverflow.com/questions/2824041/dynamically-add-items-to-tkinter-canvas
effbot.org/tkinterbook/canvas.htm#performance-issues