0

Good morning all,

I have a problem with my code. I get a list from a file.txt. I would like that with the Tkinter create line tool, create a drawing automatically.

Note that my list can contain 4 items, like 30 items.

So I would like to browse my entire list until the end.

Here is the code:

from tkinter import *

# Fenêtre
root = Tk()
Largeur = 1200
Hauteur = 800

canvas = Canvas(root, width=Largeur, height=Hauteur, background="white")
canvas.pack(side=LEFT, padx=5, pady=5)
root.resizable(width=False, height=False)
root.title('Heating Draw')

with open("valeur_x_y.txt", "r+") as file:
    liste_complet = file.readlines()
    file.close()

canvas.create_line(liste_complet[0], liste_complet[1], liste_complet[2], liste_complet[3], fill="red", width=3)
canvas.create_line(liste_complet[2], liste_complet[3], liste_complet[4], liste_complet[5], fill="red", width=3)
canvas.create_line(liste_complet[4], liste_complet[5], liste_complet[6], liste_complet[7], fill="red", width=3)

root.mainloop()

Can you help me?

Thank you

2
  • You want something like that: reply.it Commented Apr 25, 2020 at 20:23
  • What kind of help do you need? What is stopping you from creating the lines in a loop? Commented Apr 25, 2020 at 21:06

2 Answers 2

1

This is what i came up with. Assuming you formated the .txt file like this:

10, 10
20, 20
20, 20
40, 30
40, 30
80, 40
...
from tkinter import *

# Fenêtre
root = Tk()
Largeur = 1200
Hauteur = 800

canvas = Canvas(root, width=Largeur, height=Hauteur, background="white")
canvas.pack(side=LEFT, padx=5, pady=5)
root.resizable(width=False, height=False)
root.title('Heating Draw')

with open("valeur_x_y.txt", "r+") as file:
    liste_complet = file.readlines()
    liste_complet = ", ".join(liste_complet) # join it to a string
    liste_complet = liste_complet.replace("\n", "") # make it only 1-lined
    liste_complet = liste_complet.split(", ") # back to array where one value is one number
    file.close()

i = 0 # declare an index for later drawing

while(i <= len(liste_complet)-3): # going trough everything ("-3" preventing index out of range error)
    canvas.create_line(liste_complet[i], liste_complet[i+1], liste_complet[i+2], liste_complet[i+3], fill="red", width=3) # adding 1 to i for the next position
    i += 2 # adding 2 to i, because 1 position consists of 2 values

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

I just tested, it works perfectly.

Now, from the loop you made, I would like to arrive at the result below. I had coded everything with the Turtle module but I can't coexist Turtle and Tkinter on the same window, that's why I told myself that I was going to do everything with Tkinter.

Here is the Turtle code with the expected end result.

from turtle import *

screen = Screen()

# Paramètre Turtle line1 et line2
line1 = Turtle()
line2 = Turtle()

# Définition des variables modifiables
color_table=["blue", "red"]
star_x = -160
star_y = 10
line1_size_x = 200
#line1_size_y = 200
line_pensize = 3
rotation_line = 90
entre_line = 20

# Import de la ligne1
line1.hideturtle()
line1.color(color_table[1])
line1.pensize(line_pensize)

#Import de la ligne 2:
line2.hideturtle()
line2.pensize(line_pensize)
line2.color(color_table[0])
line2_size = line1_size_x - entre_line

# Position de la ligne1
line1.penup()
line1.setposition(star_x, star_y)
line1.pendown()

# Boucle de traçage automatique
while True:
    line1.forward(line1_size_x)
    line1.left(rotation_line)
    line1_size_x -= entre_line / 2
    if line1_size_x <= entre_line / 2:
        break

# Position de la ligne 2
line2.penup()
line2.setposition(star_x + entre_line / 2, star_y + entre_line / 2)
line2.pendown()

while True:
    line2.forward(line2_size)
    line2.left(rotation_line)
    line2_size -= entre_line / 2
    if line2_size <= entre_line / 2:
        break

screen.mainloop()

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.