Created
March 22, 2013 18:38
-
-
Save jarobins/5223671 to your computer and use it in GitHub Desktop.
Test out tk moving objects around on a canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: tk_move.py | |
# Programmer: Jake Robinson | |
# Date: 07MAR2013 | |
from Tkinter import * | |
import datetime | |
global x, y, start_time | |
x, y = 0, 0 | |
start_time = datetime.datetime(100,1,1,11,34,59) | |
class App: | |
def __init__(self, master): | |
global x, y | |
master.maxsize(400, 220) | |
frame = Frame(master) | |
frame.pack() | |
self.canvas_frame = Frame(frame, width=200, height=200) | |
self.canvas_frame.pack() | |
self.canvas = Canvas(self.canvas_frame, width=100000, height=200, | |
scrollregion=(0,0,100000,0), bg='white') | |
self.canvas.pack() | |
for x in range(10): | |
self.canvas.create_oval(50+(x*30), 50, 70+(x*30), 70, | |
fill='black', tags='circle%s' % x) | |
self.canvas.bind('<Control-Motion>', self.xy_motion) | |
hbar=Scrollbar(frame,orient=HORIZONTAL) | |
hbar.pack(side=BOTTOM,fill=X) | |
hbar.config(command=self.canvas.xview) | |
self.canvas.config(xscrollcommand=hbar.set) | |
for ids in self.canvas.find_all(): | |
print self.canvas.gettags(ids)[0] | |
print self.canvas.bbox(ids) | |
self.canvas.bind('<ButtonPress-1>', self.test) | |
self.canvas.bind('<ButtonRelease-1>', self.get_cords) | |
def move_it(self, canvas, circle_id, x, y): | |
old_cords = canvas.coords(circle_id) | |
canvas.move(circle_id, x - (old_cords[0]+old_cords[2])/2, | |
y - (old_cords[1]+old_cords[3])/2) | |
def get_cords(self, event): | |
global circle_id | |
canvas = event.widget | |
x = canvas.canvasx(event.x) | |
y = canvas.canvasy(event.y) | |
self.move_it(canvas, circle_id, x, y) | |
def xy_motion(self, event): | |
global x, y, start_time | |
canvas = event.widget | |
x = canvas.canvasx(event.x) | |
y = canvas.canvasy(event.y) | |
self.canvas.delete('line') | |
time = start_time + datetime.timedelta(microseconds=x*10000) | |
mouse_line = self.canvas.create_line(x, 10, x, 190, tags='line') | |
mouse_pos = self.canvas.create_text(x+10, 20, text=time, | |
tags='line', anchor=W) | |
def test(self, event): | |
global circle_id | |
canvas = event.widget | |
x, y = canvas.canvasx(event.x), canvas.canvasy(event.y) | |
an_id = canvas.find_overlapping(x, y, x, y) | |
circle_id = an_id | |
root = Tk() | |
app = App(root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment