Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter画布上制作许多可以稍后修改的形状_Python_Tkinter - Fatal编程技术网

Python 在tkinter画布上制作许多可以稍后修改的形状

Python 在tkinter画布上制作许多可以稍后修改的形状,python,tkinter,Python,Tkinter,我想制作一些几何单位,然后在画布上重复100次。但是,我需要能够在绘制每个单元后修改它们 我想将代码包装成一个方法,并多次调用该方法来绘制单元(现在是4弧)。但是我不能进入第一个弧,等等 def box(coords): arc_one = canvas.create_arc(coords, start=0, extent=90, outline="white", style="arc", tag=1) arc_two = canvas.create_arc(coords, s

我想制作一些几何单位,然后在画布上重复100次。但是,我需要能够在绘制每个单元后修改它们

我想将代码包装成一个方法,并多次调用该方法来绘制单元(现在是4弧)。但是我不能进入第一个弧,等等

def box(coords):
    arc_one = canvas.create_arc(coords, start=0, extent=90, outline="white", style="arc", tag=1)

    arc_two = canvas.create_arc(coords, start=90, extent=90, outline="white", style="arc", tag=2)

    arc_three = canvas.create_arc(coords, start=180, extent=90, outline="white", style="arc", tag=3)

    arc_four = canvas.create_arc(coords, start=270, extent=90, outline="white", style="arc", tag=4)
^想法,但我以后无法访问
arc\u one

目前我有:

import tkinter as tk
import time

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

# direction will be dynamic later
direction='RIGHT'

# motion will be dynamic later
motion='FULLMOON'

# each bounding box is 100 x 100
# coordinates are (x,y) of upper left corner, and then (x,y) of lower left corner

coords = (100, 50, 200, 150)

# use box width to later move around
box_width = coords[2] - coords[0]

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

arc_one = canvas.create_arc(coords, start=0, extent=90, outline="white", style="arc",tag=1)

arc_two = canvas.create_arc(coords, start=90, extent=90, outline="white", style="arc",tag=2)

arc_three = canvas.create_arc(coords, start=180, extent=90, outline="white", style="arc",tag=3)

arc_four = canvas.create_arc(coords, start=270, extent=90, outline="white", style="arc",tag=4)

'''
I want to just bundle arcs one to four into a single method, and call it with new coordinates to create many new 
circles. 

However, I won't be able to call each arc later if I do so.
'''

# second bounding box to the right
coords_2 = (100 + 100, 50, 200 + 100, 150)

arc_five = canvas.create_arc(coords_2, start=0, extent=90, outline="white", style="arc",tag=1)

arc_six = canvas.create_arc(coords_2, start=90, extent=90, outline="white", style="arc",tag=2)

arc_seven = canvas.create_arc(coords_2, start=180, extent=90, outline="white", style="arc",tag=3)

arc_eight = canvas.create_arc(coords_2, start=270, extent=90, outline="white", style="arc",tag=4)


def bold(event):
    id = event.widget.find_closest(event.x,event.y)[0]
    canvas.itemconfigure(id,width=2.5)
    canvas.update()
    time.sleep(.5)

    if direction == 'RIGHT' and motion == 'FULLMOON':
#         while there are no more new widgets
        while (id != event.widget.find_closest(event.x + arc_width, event.y)[0]):
    #         move cursor to the right
            event.x += arc_width
            id = event.widget.find_closest(event.x, event.y)[0]
            canvas.itemconfigure(id, width=2.5)
            canvas.update()
            time.sleep(.5)


canvas.tag_bind(arc_one,"<Button-1>", bold)
canvas.tag_bind(arc_two,"<Button-1>", bold)
canvas.tag_bind(arc_three,"<Button-1>", bold)
canvas.tag_bind(arc_four,"<Button-1>", bold)

canvas.tag_bind(arc_five,"<Button-1>", bold)
canvas.tag_bind(arc_six,"<Button-1>", bold)
canvas.tag_bind(arc_seven,"<Button-1>", bold)
canvas.tag_bind(arc_eight,"<Button-1>", bold)


root.mainloop()

将tkinter作为tk导入
导入时间
root=tk.tk()
canvas=tk.canvas(根,宽度=300,高度=200,bg='black')
canvas.pack(fill=“both”,expand=True)
#以后的方向将是动态的
方向‘‘右’
#运动稍后将是动态的
“满月”运动
#每个边界框为100 x 100
#坐标是左上角的(x,y),然后是左下角的(x,y)
坐标=(10050200150)
#使用“长方体宽度”可在以后四处移动
方框宽度=坐标[2]-坐标[0]
#使用弧宽度作为1个构件的宽度
#1盒4个组件
弧形宽度=方框宽度/2
弧形=画布。创建弧形(坐标,起点=0,范围=90,轮廓=白色,样式=弧形,标记=1)
弧二=画布。创建弧(坐标,起点=90,范围=90,轮廓=“白色”,样式=“弧”,标记=2)
弧三=画布。创建弧(坐标,起点=180,范围=90,轮廓=白色,样式=弧,标记=3)
弧四=画布。创建弧(坐标,起点=270,范围=90,轮廓=白色,样式=弧,标记=4)
'''
我只想将一到四个圆弧捆绑到一个方法中,并使用新坐标调用它来创建许多新方法
圈子。
然而,如果我这样做的话,我将无法在以后调用每个arc。
'''
#右侧的第二个边界框
坐标2=(100+100,50200+100150)
arc_five=画布。创建_arc(坐标_2,开始=0,范围=90,outline=“white”,style=“arc”,tag=1)
arc_六=画布。创建_弧(坐标_二,开始=90,范围=90,轮廓=“白色”,样式=“圆弧”,标记=2)
arc_seven=画布。创建_弧(坐标_2,开始=180,范围=90,outline=“white”,style=“arc”,tag=3)
圆弧=画布。创建圆弧(坐标为2,起点为270,范围为90,轮廓为白色,样式为圆弧,标记为4)
def bold(事件):
id=event.widget.find_最近(event.x,event.y)[0]
canvas.itemconfigure(id,宽度=2.5)
canvas.update()
时间。睡眠(.5)
如果方向='右',运动='满月':
#虽然没有更多的新部件
while(id!=event.widget.find_-closest(event.x+arc_-width,event.y)[0]):
#向右移动光标
事件x+=弧的宽度
id=event.widget.find_最近(event.x,event.y)[0]
canvas.itemconfigure(id,宽度=2.5)
canvas.update()
时间。睡眠(.5)
canvas.tag_bind(弧一,“,粗体)
canvas.tag_bind(弧二,“,粗体)
canvas.tag_bind(弧三,“,粗体)
canvas.tag_bind(弧四,“,粗体)
canvas.tag_bind(弧五,“,粗体)
canvas.tag_bind(弧六,“,粗体)
canvas.tag_bind(弧七,“,粗体)
canvas.tag_bind(弧八,“,粗体)
root.mainloop()

我建议将
更改为类。这将使我们更容易将每个“框”视为一个对象,并且每个对象都有一个名称空间来保存自己的元数据。这正是设计类要解决的问题类型

在下面的示例中,“框”中的每个弧都有两个标记。一个是
“arc”
,以防您希望能够一次更改所有框的所有圆弧,另一个是每个框的唯一标记,用于为单个框一次配置所有圆弧

此外,每个弧的画布ID保存在一个数组中。有了它,您可以配置任何单个“框”的任何单个圆弧

使用示例:

box1 = Box(canvas, (100, 50, 200, 150))
box2 = Box(canvas, (100 + 100, 50, 200 + 100, 150))
要全局修改所有圆弧,可以配置“圆弧”标记:

要修改单个框,请使用该框的标记:

canvas.itemconfigure(box1.tag, outline='red')
要修改单个长方体的单个圆弧,请使用该圆弧对该长方体的索引

canvas.itemconfigure(box2.arcs[0], outline="green")
如果执行上述所有操作,最终结果将如下图所示,其中:

  • 黄色表示我们对所有圆弧所做的更改
  • 红色表示我们对方框1所做的更改,以及
  • 绿色表示我们在
    box2

如果希望能够单击任何单个弧,则只需在
“arc”
标记上有一个bind语句:

canvas.tag_bind("arc", "<Button-1>", bold)
canvas.tag\u bind(“弧”、“粗体”)
你觉得怎么样
canvas.itemconfigure(box2.arcs[0], outline="green")
canvas.tag_bind("arc", "<Button-1>", bold)