Python GUI Tk : Canvas
Canvas: Simple plot
from Tkinter import * def main(): root = Tk() root.title('Simple Plot - Version 3 - Smoothed') try: canvas = Canvas(root, width=450, height=300, bg = 'white') canvas.pack() Button(root, text='Quit', command=root.quit).pack() canvas.create_line(100,250,400,250, width=2) canvas.create_line(100,250,100,50, width=2) for i in range(11): x = 100 + (i * 30) canvas.create_line(x,250,x,245, width=2) canvas.create_text(x,254, text='%d'% (10*i), anchor=N) for i in range(6): x = 250 - (i + 40) canvas.create_line(100,y,105,y, width=2) canvas.create_text(96,y, text='%5.1f'% (50.*i), anchor=E) scaled = [] for x,y in [(12, 56), (20, 94), (33, 98), (45, 120), (61, 180), (75, 160), (98, 223)]: scaled.append(100 + 3*x, 250 - (4*y)/5) canvas.create_line(scaled, fill='black', smooth=1) for xs,ys in scaled: canvas.create_oval(x-6,y-6,x+6,y+6, width=1, outline='black', fill='SkyBlue2') except: print 'An error has occured!' root.mainloop() main()
Bound Scale action with a canvas
from Tkinter import * import string def setHeight(canvas, heightStr): height = string.atoi(heightStr) height = height + 21 y2 = height - 30 if y2 < 21: y2 = 21 canvas.coords('poly', 15,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20) canvas.coords('line', 15,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20) root = Tk() root.title('Scale') canvas = Canvas(root, width=50, height=50, bd=0, highlightthickness=0) canvas.create_polygon(0,0,1,1,2,2, fill='cadetblue', tags='poly') canvas.create_line(0,0,1,1,2,2,0,0, fill='black', tags='line') scale = Scale(root, orient=VERTICAL, length=284, from_=0, to=250, tickinterval=50, command=lambda h, c=canvas:setHeight(c,h)) scale.grid(row=0, column=0, sticky='NE') canvas.grid(row=0, column=1, sticky='NWSE') scale.set(100) root.mainloop()
Canvas inside a frame
from Tkinter import * class AllTkinterWidgets: def __init__(self, master): frame = Frame(master, width=500, height=400, bd=1) frame.pack() iframe5 = Frame(frame, bd=2, relief=RAISED) iframe5.pack(expand=1, fill=X, pady=10, padx=5) c = Canvas(iframe5, bg='white', width=340, height=100) c.pack() for i in range(25): c.create_oval(5+(4*i),5+(3*i),(5*i)+60,(i)+60, fill='gray70') c.create_text(260, 80, text='Canvas', font=('verdana', 10, 'bold')) iframe5.pack(expand=1, fill=X, pady=10, padx=5) root = Tk() #root.option_add('*font', ('verdana', 10, 'bold')) all = AllTkinterWidgets(root) root.title('Tkinter Widgets') root.mainloop()
Use canvas to draw line, oval, rectangle, bitmap and arc
from Tkinter import * root = Tk() root.option_readfile('optionDB') root.title('Canvas') canvas = Canvas(root, width =400, height=400) canvas.create_oval(10,10,100,100, fill='gray90') canvas.create_line(105,10,200,105, stipple='@bitmaps/gray3') canvas.create_rectangle(205,10,300,105, outline='white', fill='gray50') canvas.create_bitmap(355, 53, bitmap='questhead') xy = 10, 105, 100, 200 canvas.create_arc(xy, start=0, extent=270, fill='gray60') canvas.create_arc(xy, start=270, extent=5, fill='gray70') canvas.create_arc(xy, start=275, extent=35, fill='gray80') canvas.create_arc(xy, start=310, extent=49, fill='gray90') canvas.create_polygon(205,105,285,125,166,177,210,199,205,105, fill='white') canvas.create_text(350,150, text='text', fill='yellow', font=('verdana', 36)) img = PhotoImage(file='./logo.gif') canvas.create_image(145,280, image=img, anchor=CENTER) frm = Frame(canvas, relief=GROOVE, borderwidth=2) Label(frm, text="Embedded Frame/Label").pack() canvas.create_window(285, 280, window=frm, anchor=CENTER) canvas.pack() root.mainloop()
Bind mouse click action to the object on a canvas
from Tkinter import * def onObjectClick(event): print 'Clicked', event.x, event.y, event.widget, print event.widget.find_closest(event.x, event.y) root = Tk() canv = Canvas(root, width=100, height=100) obj1 = canv.create_text(50, 30, text='Click me one') obj2 = canv.create_text(50, 70, text='Click me two') canv.tag_bind(obj1, '<Double-1>', onObjectClick) canv.tag_bind(obj2, '<Double-1>', onObjectClick) canv.pack() root.mainloop()
Bind action to canvas
from Tkinter import * def onCanvasClick(event): print 'Got canvas click', event.x, event.y, event.widget root = Tk() canv = Canvas(root, width=100, height=100) obj1 = canv.create_text(50, 30, text='one') obj2 = canv.create_text(50, 70, text='two') canv.bind('<Double-1>', onCanvasClick) canv.pack() root.mainloop()
Use mouse to draw a shape on canvas
from Tkinter import * trace = 0 class CanvasEventsDemo: def __init__(self, parent=None): canvas = Canvas(width=300, height=300, bg='beige') canvas.pack() canvas.bind('<ButtonPress-1>', self.onStart) canvas.bind('<B1-Motion>', self.onGrow) canvas.bind('<Double-1>', self.onClear) canvas.bind('<ButtonPress-3>', self.onMove) self.canvas = canvas self.drawn = None self.kinds = [canvas.create_oval, canvas.create_rectangle] def onStart(self, event): self.shape = self.kinds[0] self.kinds = self.kinds[1:] + self.kinds[:1] self.start = event self.drawn = None def onGrow(self, event): canvas = event.widget if self.drawn: canvas.delete(self.drawn) objectId = self.shape(self.start.x, self.start.y, event.x, event.y) if trace: print objectId self.drawn = objectId def onClear(self, event): event.widget.delete('all') def onMove(self, event): if self.drawn: if trace: print self.drawn canvas = event.widget diffX, diffY = (event.x - self.start.x), (event.y - self.start.y) canvas.move(self.drawn, diffX, diffY) self.start = event if __name__ == '__main__': CanvasEventsDemo() mainloop()
Image canvas
gifdir = "./" from sys import argv from Tkinter import * filename = 'logo.gif' win = Tk() img = PhotoImage(file=gifdir+filename) can = Canvas(win) can.pack(fill=BOTH) can.config(width=img.width(), height=img.height()) can.create_image(2, 2, image=img, anchor=NW) win.mainloop()