1

I've been trying to create a graph using a create_line and a list of (x,y) points.

import Tkinter
Screen = [a list of screen coordinates]
World = []
for x,y in screen:
    World.append(somefunctiontochange(x,y))
    if len(World) >= 2:
        Canvas.create_line(World)

The line doesn't show in my canvas though, and no error was given. Any help?

4
  • You should mention that you're using Tkinter (I think?) Commented May 11, 2013 at 7:36
  • Yeah i have all those. This is just a segment of my overall code. Ill edit it. Commented May 11, 2013 at 7:46
  • For code to show properly on this site, you need to indent it by 4 spaces. Commented May 11, 2013 at 7:51
  • yeah. but could you answer my question? Commented May 11, 2013 at 8:00

2 Answers 2

2

Took me a while but this is how you draw to a canvas in the way you want:

import Tkinter as tk

root = tk.Tk()
root.geometry("500x500")
root.title("Drawing lines to a canvas")

cv = tk.Canvas(root,height="500",width="500",bg="white")
cv.pack()

def linemaker(screen_points):
    """ Function to take list of points and make them into lines
    """
    is_first = True
    # Set up some variables to hold x,y coods
    x0 = y0 = 0
    # Grab each pair of points from the input list
    for (x,y) in screen_points:
        # If its the first point in a set, set x0,y0 to the values
        if is_first:
            x0 = x
            y0 = y
            is_first = False
        else:
            # If its not the fist point yeild previous pair and current pair
            yield x0,y0,x,y
            # Set current x,y to start coords of next line
            x0,y0 = x,y

list_of_screen_coods = [(50,250),(150,100),(250,250),(350,100)]

for (x0,y0,x1,y1) in linemaker(list_of_screen_coods):
    cv.create_line(x0,y0,x1,y1, width=1,fill="red")

root.mainloop()

You need to supply create_line with the x,y positions at the start and end point of the line, in the example code above (works) I'm drawing four lines connecting points (50,250),(150,100),(250,250),(350,100) in a zigzag line

Its worth pointing out also that the x,y coords on a canvas start at the top left rather than the bottom left, think of it less like a graph with the x,y = 0,0 in the bottom left of the canvas and more how you would print to a page starting in top left corner moving to the right in the x and with the y incrementing as you move down the page.

I used: http://www.tutorialspoint.com/python/tk_canvas.htm as reference.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. But i was thinking about rather a set of lines connecting to each other by points. Like a graph.
1

If you aren't getting errors and you're certain your function is being called, you probably have one of three problems:

Is your canvas visible? A common mistake for beginners is to either forget to pack/grid/place the canvas, or to neglect to do that for all of its containers. An easy way to verify is to temporarily give your canvas a really bright background so that it stands out from the rest of the GUI.

Have you set the scroll region? The other explanation is that the drawing is happening, but it's happening in an area that is outside the viewable portion of the canvas. You should be setting the scrollregion attribute of the canvas after creating your drawings, to make sure everything you're drawing can be made visible.

Does your canvas and canvas objects have an appropriate color? It's possible that you've changed the background of the canvas to black (since you don't show that code in your question), and you're using a default color of black when creating your lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.