Home > Software design >  Python TKinter - Moving polygon objects drawn within a canvas
Python TKinter - Moving polygon objects drawn within a canvas

Time:11-18

I am new to Tkinter programming and have been asked to write a programme that uses buttons to draw a polygon on a canvas and then be able to click and drag those polygon into position. I am able to draw the polygon with a function linked to the "get domain" button and I have used a canvas.tag_bind to that block to bind mouseclicks to moving the polygon. My issue is when I click and drag the polygon it moves the whole canvas that the polygon is drawn on. Is there a way in this code to specify how to move only the polygon?

#!/usr/bin/python
import re
import sys
import os
import tkinter as tk

HEIGHT = 1200
WIDTH  = 1600

def domain_button(canvas):
    domain  = canvas.create_polygon([420, 100, 420, 100, 420, 175, 420, 175, 380, 175, 380, 120, 420, 120], outline='#000000',fill='#007ECB', width=2, tags="obj1Tag")
    canvas.tag_bind(domain,"<Button-1>", drag_start)
    canvas.tag_bind(domain,"<B1-Motion>", drag_motion)

def button_hover(e):
    button["bg"] = "white"
    status_label.config(text="Get Input")
def button_hover_leave(e):
    button["bg"] = "grey"
    status_label.config(text="")
def button_hover_polygon(label):
    status_label.config(text=label)
def button_hover_polygon_leave(e):
    status_label.config(text="")
def drag_start(e):
    domain = e.widget
    domain.startX = e.x
    domain.startY = e.y

def drag_motion(e):
    domain = e.widget
    x = domain.winfo_x() - domain.startX   e.x
    y = domain.winfo_y() - domain.startY   e.y
    domain.place(x=x,y=y)



###############Main programme#######################

root = tk.Tk()

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH, bg='#E7E0E6')
canvas.pack()

frame = tk.Frame(root, bg = '#80c1ff',bd=5)
frame.place(relwidth=1,relheight=1)

###Input box
textBox = tk.Text(frame, font=40)
textBox.place(relx=0.01,rely = 0.05, relwidth=0.4,relheight=0.3)

###Option box
frame2 = tk.Frame(frame, bg = '#D3D3D3')
frame2.place(relx=0.01, rely = 0.45,relheight = 0.35, relwidth = 0.4)
InsertVLDomainButton= tk.Button(frame2,text="insert domain",bg = "grey", command=lambda: domain_button(lower_canvas))
InsertVLDomainButton.place(relx = 0.01, rely = 0.01, relheight = 0.2, relwidth=0.2)


status_label = tk.Label(root, text='test', bd=1)
status_label.place(rely = 0.98, relheight = 0.02, relwidth = 1)

##Big button
button = tk.Button(frame, text = "Go!", bg = "grey", font=40, command=lambda: pipeline(lower_canvas))
button.place(relx=0.1,rely=0.85,relheight=0.1, relwidth=0.2)
button.bind("<Enter>", button_hover)
button.bind("<Leave>", button_hover_leave)

###Results canvas

lower_frame = tk.Frame(root, bg = '#80c1ff', bd=10)
lower_frame.place(relx=0.45, rely=0.05, relwidth=0.55,relheight=0.9)

lower_canvas = tk.Canvas(lower_frame)
lower_canvas.place(relheight=1,relwidth=1)




root.mainloop()

I'm not sure if I am missing something obvious. I admit I took the functions to move the polygons from online tutorials.

Thank you in advance!

CodePudding user response:

Here is something that might do a trick for you. It is simple create button and drag it code:

from tkinter import *
from tkinter.dnd import Tester as DragWindow, Icon as Draggable

root = Tk()
root.geometry("800x600")
root.withdraw()  # hide root window
# Make the actual main window, which can have draggable objects on.
main = DragWindow(root)


def make_btn():
    """Make a new test button."""
    # The functional part of the main window is the canvas.
    Draggable('Button').attach(main.canvas)


Button(main.top, text='Create New Button', command=make_btn).pack()
mainloop()
  • Related