0

I am coding a simulation of a fire in a forest. The code is mostly done and the result can be seen below. To start the fire, you have to input the coordinates of the first tree that will burn, so that it can spread around.

However, python will ask you again and again about these coordinates if a tree is not found in the designated area. Hence, I am looking to create a function that would help me to choose the first tree by clicking on it in a tkinter window and not lose time finding a tree.

You will find below the part of the code that asks about the coordinates. If needed, I can post the whole code.

Thanks.

def take_input():
while 1:
    print("The coordinates to start the fire are :")
    debut =int(input("x= "))
    debutbis=int(input("y= "))
    xx=T[debut]
    yy=T[debut][debutbis]
    if yy == 0:
        print("Error, no tree found.")
    elif debut < 0 or debut >= len(T) or debutbis < 0 or debutbis >= len(T[0]):
        print("Error")
    else:
        break
app.after(200, burn_forest, debut, debutbis)
4
  • How are your trees represented on the canvas? Commented May 3, 2018 at 6:46
  • The full code is here mediafire.com/?53fao238x53ty Commented May 3, 2018 at 6:47
  • By small green squares @ReblochonMasque Commented May 3, 2018 at 6:49
  • give a 'tree' tag to your small green squares, then use the answer posted here: stackoverflow.com/questions/15565809/… Commented May 3, 2018 at 7:03

1 Answer 1

1

Since you are not explaining how you display trees I'll give you two examples. First how to get the coordinates of a mouse cilck:

from tkinter import *

root = Tk()
root.geometry('200x200')

def click(event):
    print(event.x,event.y)

root.bind('<Button-1>', click)
root.mainloop()

Second, a way to pick objects on a canvas:

import tkinter as tk
import random

def on_click(event):
    current = event.widget.find_withtag("current")
    if current:
        item = current[0]
        color = canvas.itemcget(item, "fill")
        label.configure(text="you clicked on item with id %s (%s)" % (item, color))
    else:
        label.configure(text="You didn't click on an item")

root = tk.Tk()
label = tk.Label(root, anchor="w")
canvas = tk.Canvas(root, background="bisque", width=400, height=400)
label.pack(side="top", fill="x")
canvas.pack(fill="both", expand=True)

for color in ("red", "orange", "yellow", "green", "blue", "violet"):
    x0 = random.randint(50, 350)
    y0 = random.randint(50, 350)
    canvas.create_rectangle(x0, y0, x0+50, y0+50, outline="black", fill=color)
    canvas.bind('<ButtonPress-1>', on_click)

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

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.