Skip to content

Instantly share code, notes, and snippets.

@jarobins
Created March 22, 2013 18:35
Show Gist options
  • Save jarobins/5223658 to your computer and use it in GitHub Desktop.
Save jarobins/5223658 to your computer and use it in GitHub Desktop.
Testing out some Tk functionality creating a cursor and custom widgets.
# File: time_cursor.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)
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('<1>', self.test)
self.canvas.bind('<ButtonRelease-1>', self.hello)
def hello(self, event):
print 'hello'
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):
canvas = event.widget
x, y = canvas.canvasx(event.x), canvas.canvasy(event.y)
an_id = canvas.find_overlapping(x, y, x, y)
if 'red' in canvas.itemconfigure(an_id)['fill']:
canvas.itemconfigure(an_id, fill='black')
else:
canvas.itemconfigure(an_id, fill='red')
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