user1779067
user1779067

Reputation: 21

Tkinter draws in two canvases

I'm writing 3 functions in Tkinter. Each function is in the form ObjectName(c,x,y) where c is the name of the canvas. I want each function to draw shape in any given canvas. Example:

from Tkinter import *
root = Tk()

def line(c,x,y):
    root = Tk()
    c = Canvas(root, width=600, height=800)
    c.pack()
    c.create_line(x-160,y,x+300,y)

drawLine(c,200,300)
root.mainloop() 

Problem: when I call the same function to draw two shapes on the same canvas it draws on two different canvases :(

Upvotes: 2

Views: 4287

Answers (1)

korhadris
korhadris

Reputation: 157

Your code looks to be creating a new canvas object each time you call line (or drawLine, as your function name and usage appears to be inconsistent.) You shouldn't create a new root object and Canvas object in your function.

Try something like this:

from Tkinter import *

def drawLine(c, x, y):
    c.create_line(x - 160, y, x + 300, y)

root = Tk()
c = Canvas(root, width=600, height=800)
c.pack()

drawLine(c, 200, 300)
drawLine(c, 300, 400)
drawLine(c, 350, 450)

root.mainloop()

Upvotes: 1

Related Questions