Python 要传递到tkinter的内容请参见附件
我想查看当前选定对象旁边存在哪些对象(在本例中为圆弧)。我想我可以使用Python 要传递到tkinter的内容请参见附件,python,tkinter,Python,Tkinter,我想查看当前选定对象旁边存在哪些对象(在本例中为圆弧)。我想我可以使用event.widget.find_included(coords)和coords来实现这一点,coords表示一个包含4个整数的元组。但是,每次尝试传入元组时,我都会收到以下错误: TypeError:find_enclosed()缺少3个必需的位置参数:“y1”、“x2”和“y2” 完整代码如下: import tkinter as tk import time import numpy root = tk.Tk() c
event.widget.find_included(coords)
和coords
来实现这一点,coords表示一个包含4个整数的元组。但是,每次尝试传入元组时,我都会收到以下错误:
TypeError:find_enclosed()缺少3个必需的位置参数:“y1”、“x2”和“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()
将tkinter作为tk导入
导入时间
进口numpy
root=tk.tk()
canvas=tk.canvas(根,宽度=500,高度=200,bg='black')
canvas.pack(fill=“both”,expand=True)
#对行应用粗体
def bold(事件):
#最初的大胆
#查找arc用户名称
id=event.widget.find_最近(event.x,event.y)[0]
#检索弧标记
tag=canvas.gettags(id)[1]
打印(标签)
打印(类型(标签))
#粗弧
canvas.itemconfigure(id,宽度=5)
#重画画布
canvas.update()
#给点时间把每一张画成零碎的
时间。睡眠(.5)
当前_coords=numpy.array(canvas.coords(id))
next\u coords\u additive=numpy.array([arc\u width,0,arc\u width,0])
下一个坐标=当前坐标+下一个坐标
#获取下一个圆弧的数据
next_arc_data=event.widget.find_included(元组(next_坐标))
#代码以查找粗体和下一个可用弧
#每个边界框为100 x 100
类框():
定义初始化(self,coords):
#给类一个标签,以便tkinter稍后查找
self.tag='box{}'。格式(id(self))
#做每一个弧
self.arcs=[
#弧1
canvas.create_弧(coords,start=0,extent=90,outline=“white”,style=“arc”,tag=(self.tag,1)),
#弧2
canvas.create_弧(coords,start=90,extent=90,outline=“white”,style=“arc”,tag=(self.tag,2)),
#弧3
canvas.create_弧(coords,start=180,extent=90,outline=“white”,style=“arc”,tag=(self.tag,3)),
#弧4
canvas.create_弧(coords,start=270,extent=90,outline=“white”,style=“arc”,tag=(self.tag,4))
]
#允许每个弧以粗体显示
self.bind()
def绑定(自):
#对框中的每个弧应用绑定
对于self.arcs中的圆弧:
canvas.tag_bind(弧形,“,粗体)
#坐标是左上角的(x,y),然后是左下角的(x,y)
#使用numpy数组进行向量加法
coords=numpy.array([0,0,100100])
#使用“框宽度”计算栅格指示
方框宽度=坐标[2]-坐标[0]
#网格指示移动
网格标识=方框宽度/2
#使用弧宽度作为1个构件的宽度
#1盒4个组件
弧形宽度=方框宽度/2
#制作所需的网格尺寸(宽度、高度)
大小=[6,4]
对于范围内的i(尺寸[1]):
#向下移动时,继续向y轴添加1个栅格标记
coords=coords+numpy.array([0,0+栅格索引,0,0+栅格索引])
对于范围内的j(尺寸[0]):
#向右移动时,继续向x添加1个栅格标记
box\u coords=coords+numpy.array([0+grid\u indicate*j,0,0+grid\u indicate*j,0])
#创建变量以检查奇偶校验
偶数行=i%2==0
奇数行=非偶数行
偶数列=j%2==0
奇数列=非偶数列
#仅在i和j的相同奇偶性上绘制一个框
#即:在奇数行(i)上,仅绘制奇数列(j)值
如果偶数行和偶数列:
盒(元组(盒坐标))
elif奇数行和奇数列:
盒(元组(盒坐标))
root.mainloop()
不要传递元组,传递四个不同的坐标。假设next\u coords
包含四个坐标,您可以使用解包操作符*
解包坐标列表:
next_arc_data = event.widget.find_enclosed(*next_coords)
阅读,谢谢。我使用了tuple,但一直在接收()或一个空集。当我将协调员的范围扩大到(0,010001000)时,我开始接收数据。那么,相对于画布,要传入x1、y1、x2、y2的参数是什么,其中x1和y1是左上点,x2和y2是右下点?@ChristinaZhou:是的,它们代表画布的一个区域,所以它们必须在画布坐标中。奇数。我发现方法
find\u included
非常简单,我总是需要使边界框比我的事件稍微大一点。也就是说,如果我的事件存在于100,50200150中,我需要通过99,49201151,它才能找到它…@ChristinaZhou:我不知道你所说的“事件存在于100,50200150中”是什么意思。事件具有单个x/y坐标,而不是坐标范围。但是是的,它是非常字面的。项目必须完全由给定坐标包围。如果要查找仅部分位于给定区域内的对象,则存在find_overlapping
。对不起,我指的是对象,而不是事件。