同一画布上有多个图形

时间:2019-04-04 09:18:00

标签: python-3.x tkinter tkinter-canvas

我想在按下按钮时使用tkinter Python在同一画布上绘制多个图形,即第一次按下按钮时我要显示image1,第二次按下按钮时我要显示image2,依此类推。与此并行,我在画布旁边的树视图中添加图像的名称。我还想拥有一个功能,当我单击任何树视图条目时,都应该在画布上绘制该特定图像,而其他图像/图应该在内存中。使用的代码如下:

def canvas_pic():
    # asking the user to select the image file using tkinter "askopenfilename"
    canvas_pic.counter += 1
    tree.insert("", canvas_pic.counter, "dir"+str(canvas_pic.counter), 
         text="Image"+str(canvas_pic.counter))
    ax = fig.add_subplot(111)
    ax.imshow(image)
    canvas.draw()

fig = Figure(figsize=(15, 2), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

任何帮助将不胜感激。

如“ ZF007”所建议,该解决方案对我有效,有效的解决方案是:

def canvas_pic():
    # asking the user to select the image file using tkinter "askopenfilename"
    canvas_pic.counter += 1
    tree.insert("", canvas_pic.counter, "dir"+str(canvas_pic.counter), 
         text="Image"+str(canvas_pic.counter))

    global dct_array
    if (canvas_pic.counter == 1):
        dct_array = {}

    dct_array["dir"+str(canvas_pic.counter)] = image as an array loaded above

def image_draw():
    rowno = 'selected treeview element'
    image_display = dct_array[rowno]
    ax = fig.add_subplot(111)
    ax.imshow(image_display, cmap='gist_rainbow')
    ax.set(title="",xticks=[], yticks=[])
    canvas.draw() 

1 个答案:

答案 0 :(得分:0)

如“ ZF007”所建议,该解决方案对我有效,有效的解决方案是:

def canvas_pic():
    # asking the user to select the image file using tkinter "askopenfilename"
    canvas_pic.counter += 1
    tree.insert("", canvas_pic.counter, "dir"+str(canvas_pic.counter), 
         text="Image"+str(canvas_pic.counter))

    global dct_array
    if (canvas_pic.counter == 1):
        dct_array = {}

    dct_array["dir"+str(canvas_pic.counter)] = image as an array loaded above

def image_draw():
    rowno = 'selected treeview element'
    image_display = dct_array[rowno]
    ax = fig.add_subplot(111)
    ax.imshow(image_display, cmap='gist_rainbow')
    ax.set(title="",xticks=[], yticks=[])
    canvas.draw()