向tkinter画布添加标签,文本框和按钮

时间:2018-06-28 15:01:07

标签: python tkinter tkinter-canvas

如何在使用tkinter画布显示图像的程序中添加标签,文本框和按钮。在该程序中,向用户显示图像。鼠标单击不同位置时会在图像上绘制线条。我想添加一个文本框和一个按钮,以便用户可以输入使用鼠标单击绘制的对象的名称。例如,输入标签:textboxforinputname__输入按钮。

from Tkinter import *
import Image, ImageTk, ImageDraw
import numpy as np

coord=[]  # for saving coord of each click position
Dict_Polygon={}   # Dictionary for saving polygon
list_of_points=[]
flag=True
label=0

def draw_lines(event):

    mouse_xy = (event.x, event.y)
    func_Draw_lines(mouse_xy)


def func_Draw_lines(mouse_xy):

    center_x, center_y = mouse_xy
    if canvas.old_coords:
            x1, y1 = canvas.old_coords
            canvas.create_line(center_x, center_y, x1, y1)

    # add clicked positions to list
    if flag==True:
        list_of_points.append(mouse_xy)
        canvas.old_coords = center_x, center_y

# Main function
if __name__ == '__main__':

    root = Tk()    
# Input image
    img = Image.open("test.jpg")         
# Draw canvas for iput image to pop up image for clicks
    filename = ImageTk.PhotoImage(img)
    canvas = Canvas(root,height=img.size[0],width=img.size[0])
    canvas.image = filename
    canvas.create_image(0,0,anchor='nw',image=filename)
    canvas.pack()
    canvas.old_coords = None
# bind function to canvas to generate event
    canvas.bind("<Button 3>", draw_lines)
    root.mainloop()

0 个答案:

没有答案