Что перейти в tkinter find_enclosed - PullRequest
0 голосов
/ 26 сентября 2019

Я хочу проверить, какие объекты (в данном случае дуги) существуют рядом с моими выбранными в данный момент объектами.Я думал, что смогу сделать это, используя event.widget.find_enclosed(coords) с coords, представляющим кортеж с 4 целыми числами.Однако каждый раз, когда я пытался передать кортеж, я получаю эту ошибку:

TypeError: find_enclosed() missing 3 required positional arguments: 'y1', 'x2', and 'y2'

Полный код ниже:

import tkinter as tk
import time
import numpy

root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=200, bg='black')
canvas.pack(fill="both", expand=True)

# apply bold to line
def bold(event):
    # initial boldness
    # find arc user meant
    id = event.widget.find_closest(event.x,event.y)[0]
    # retrieve arc tag
    tag = canvas.gettags(id)[1]
    print(tag)
    print(type(tag))
    # bold arc
    canvas.itemconfigure(id,width=5)
    # redraw canvas
    canvas.update()
    # give time to make each drawing piecemeal
    time.sleep(.5)

    current_coords = numpy.array(canvas.coords(id))
    next_coords_additive = numpy.array([arc_width,0,arc_width,0])
    next_coords = current_coords + next_coords_additive

    # grab data for next arcs
    next_arc_data = event.widget.find_enclosed(tuple(next_coords))

#     code to find the and bold the next available arc

# each bounding box is 100 x 100
class Box():
    def __init__(self, coords):
        # give the class a tag for tkinter to find later
        self.tag = 'box{}'.format(id(self))

        # make each arc
        self.arcs = [
            # arc 1
            canvas.create_arc(coords, start=0, extent=90, outline="white", style="arc", tag=(self.tag, 1)),
            # arc 2
            canvas.create_arc(coords, start=90, extent=90, outline="white", style="arc", tag=(self.tag, 2)),
            # arc 3
            canvas.create_arc(coords, start=180, extent=90, outline="white", style="arc", tag=(self.tag, 3)),
            # arc 4
            canvas.create_arc(coords, start=270, extent=90, outline="white", style="arc", tag=(self.tag, 4))
        ]

        # allow each arc to be bolded
        self.bind()

    def bind(self):
        # apply binding to every arc in box
        for arc in self.arcs:
            canvas.tag_bind(arc, "<Button-1>", bold)

# coordinates are (x,y) of upper left corner, and then (x,y) of lower left corner
# use numpy array for vector addition
coords = numpy.array([0, 0, 100, 100])


# use box width to calculate grid indice
box_width = coords[2] - coords[0]
# grid indice to move around
grid_indice = box_width/2

# use arc width for width of 1 component
# 4 components in 1 box
arc_width = box_width/2

# make desired size of grid (width, height)
size=[6,4]

for i in range(size[1]):
    # keep adding 1 grid indice to the y as you move down
    coords = coords + numpy.array([0, 0 + grid_indice, 0, 0 + grid_indice])

    for j in range(size[0]):
        # keep adding 1 grid indice to the x as you move to the right
        box_coords = coords + numpy.array([0 + grid_indice*j, 0, 0 + grid_indice*j, 0])

        # create variables to check parity
        even_row = i%2 == 0
        odd_row = not even_row
        even_column = j%2 == 0
        odd_column = not even_column

        # only draw a box on the same parity of i and j
        # that is: on an odd row (i), only draw on odd column (j) values
        if even_row & even_column:
            Box(tuple(box_coords))
        elif odd_row & odd_column:
            Box(tuple(box_coords))


root.mainloop()

1 Ответ

0 голосов
/ 26 сентября 2019

Не передавайте кортеж, передайте четыре разные координаты.Предполагая, что next_coords содержит четыре координаты, вы можете использовать оператор распаковки *, чтобы распаковать ваш список координат:

next_arc_data = event.widget.find_enclosed(*next_coords)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...