0

When I tried to make a set of pretty little lines on a canvas in Python, I could not get my For Loop to run correctly. I intended for my For Loop to work similarly to what the commented out bit of code has done. Unfortunately, the code just didn't work at all and only caused an error message.

What I wanted to do with that For Loop was to make the variable "linecounter" to go up 20 in value 20 times, and then make the x-coordinates of the lines increase by 50 pixels to the right each time. With my current code right now, it just simply doesn't work.

Is there something in my For Loop that isn't quite written right?

import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

canvas = tk.Canvas(width=960, height=900, bg='white')
canvas.grid(row=2, column=0, columnspan=3)

##canvas.create_line(0, 50, 0, 100, width=5, fill="black")
##canvas.create_line(50, 50, 50, 100, width=5, fill="black")
##canvas.create_line(100, 50, 100, 100, width=5, fill="black")
##canvas.create_line(150, 50, 150, 100, width=5, fill="black")
##canvas.create_line(200, 50, 200, 100, width=5, fill="black")
##canvas.create_line(250, 50, 250, 100, width=5, fill="black")
##canvas.create_line(300, 50, 300, 100, width=5, fill="black")
##canvas.create_line(350, 50, 350, 100, width=5, fill="black")
##canvas.create_line(400, 50, 400, 100, width=5, fill="black")
##canvas.create_line(450, 50, 450, 100, width=5, fill="black")
##canvas.create_line(500, 50, 500, 100, width=5, fill="black")
##canvas.create_line(550, 50, 550, 100, width=5, fill="black")
##canvas.create_line(600, 50, 600, 100, width=5, fill="black")
##canvas.create_line(650, 50, 650, 100, width=5, fill="black")
##canvas.create_line(700, 50, 700, 100, width=5, fill="black")
##canvas.create_line(750, 50, 750, 100, width=5, fill="black")
##canvas.create_line(800, 50, 800, 100, width=5, fill="black")
##canvas.create_line(850, 50, 850, 100, width=5, fill="black")
##canvas.create_line(900, 50, 900, 100, width=5, fill="black")
##canvas.create_line(950, 50, 950, 100, width=5, fill="black")
##canvas.create_line(1000, 50, 1000, 100, width=5, fill="black")

for linecounter 1 to 20
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

root.mainloop()
1
  • please be sure to properly research your issues before you post questions here. Commented Sep 16, 2018 at 2:59

2 Answers 2

2

The information you need to fix your problem can be found at: https://wiki.python.org/moin/ForLoop

You should utilize Google to fix simple problems like this. You narrowed your issue down to the for-loop, and searching for "For Loops Python" could have shown you the proper way to do this.

Your for-loop needs to be rewritten as:

for linecounter in range(20):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

This will iterate from 0 to 19. To iterate from 0 to 20, which it appears you're trying to do, use:

for linecounter in range(21):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")
Sign up to request clarification or add additional context in comments.

2 Comments

Just to confirm, does "iterate from 0 to 19" mean the program will repeat 20 times?
0 to 19 will start at zero and go up to 19. So a total of 20 times. The code you commented out is actually 21 lines, as it goes from 0 to 20.
2
for linecounter in range(21):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

1 Comment

this gives 0-20, it should be range(1, 21), although I guess they do have a 0 in the first line

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.