Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_canvasitems.py ZZZ
- from Tkinter import *
- def _create_widgets(): ###ZZZ
- if canvas.isapp:
- txt = ( "This window contains a canvas widget with examples of the ",
- "various kinds of items supported by canvases. The following ",
- "operations are supported:\n",
- "\tItem Highlight:\titem under mouse pointer is highlighted.\n"
- "\tButton-1 drag:\tmoves item under pointer.")
- canvas.create_text('22.5c 9c', anchor=N, font=FONT1, width='4c',
- text=''.join(txt), tags=('item', ))
- def _create_demo_panel():
- # draw items
- _draw_lines()
- _draw_curves()
- _draw_polygons()
- _draw_rectangles()
- _draw_ovals()
- _draw_text()
- _draw_arcs()
- _draw_bitmaps()
- _draw_windows()
- _add_bindings()
- def _create_main_scrollbars():
- canvas.config(scrollregion=(0,0,1800, 1400), width='15c',
- height='10c', relief=SUNKEN, borderwidth=2)
- sbarV = Scrollbar(canvas)
- sbarV.config(command=canvas.yview)
- canvas.config(yscrollcommand=sbarV.set)
- sbarV.pack(side=RIGHT, fill=Y)
- sbarH = Scrollbar(canvas, orient=HORIZONTAL)
- sbarH.config(command=canvas.xview)
- canvas.config(xscrollcommand=sbarH.set)
- sbarH.pack(side=BOTTOM, fill=X)
- # ===================================================================================
- # Canvas item drawing routines
- # ===================================================================================
- def _draw_lines():
- # Draw a 3x3 rectangular grid.
- canvas.create_rectangle('0c 0c 30c 24c', width=2)
- canvas.create_line('0c 8c 30c 8c', width=2)
- canvas.create_line('0c 16c 30c 16c', width=2)
- canvas.create_line('10c 0c 10c 24c', width=2)
- canvas.create_line('20c 0c 20c 24c', width=2)
- canvas.create_text('5c .2c', text='Lines', anchor=N)
- canvas.create_line('1c 1c 3c 1c 1c 4c 3c 4c', width='2m',
- fill=BLUE, cap='butt', join='miter',
- tags=('item', )) # Z
- canvas.create_line('4.67c 1c 4.67c 4c', arrow=LAST,
- tags=('item', )) # line w/one arrow
- canvas.create_line('6.33c 1c 6.33c 4c', arrow=BOTH,
- tags=('item')) # line with two arrows
- # maze
- line = ['5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c ',
- '8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c']
- canvas.create_line(''.join(line), width=3, fill=RED, tags=('item', ))
- # stippled line
- canvas.create_line('1c 5c 7c 5c 7c 7c 9c 7c', width='.5c', stipple='gray25',
- arrow=BOTH, arrowshape=(15, 15, 7), tags=('item', ))
- # 'M' shape
- canvas.create_line('1c 7c 1.75c 5.8c 2.5c 7cc 3.25c 5.8c 4c 7c',
- width='.5c', cap=ROUND, join=ROUND, tags=('item', ))
- def _draw_curves():
- canvas.create_text('15c .2c', text='Curves (smoothed lines)', anchor=N)
- # curved
- canvas.create_line('11c 4c 11.5c 1c 13.5c 1c 14c 4c', smooth='on',
- fill=BLUE, tags=('item', ))
- # snake with arrows
- canvas.create_line('15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c',
- smooth='on', arrow=BOTH, width=3, tags=('item',))
- # stippled infinity
- line = ['12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c ',
- '16.5c 4.5c 13.5c 7.5c 12c 6c']
- canvas.create_line(''.join(line), smooth='on', width='3m', stipple='gray25',
- cap=ROUND, fill=RED, tags=('item', ))
- def _draw_polygons():
- canvas.create_text('25c .2c', text='Polygons', anchor=N)
- # 4 pointed star
- poly = ['21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c ',
- '24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c']
- canvas.create_polygon(''.join(poly), fill=GREEN,
- outline='black', width=4, tags=('item', ))
- # roller-coaster
- poly = ['25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c ',
- '29c 1c 29c 4c 29c 4c']
- canvas.create_polygon(''.join(poly), fill=RED, smooth='on',
- tags=('item', ))
- # stippled blocks
- poly = ['22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c ',
- '28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c ']
- canvas.create_polygon(''.join(poly), stipple='gray25',
- outline='black', tags=('item', ))
- def _draw_rectangles():
- canvas.create_text('5c 8.2c', text='Rectangles', anchor=N)
- # empty square
- canvas.create_rectangle('1c 9.5c 4c 12.5c', outline=RED,
- width='3m', tags=('item', ))
- # filled, outlined block
- canvas.create_rectangle('0.5c 13.5c 4.5c 15.5c', fill=GREEN,
- tags=('item', ))
- # stipple filled, non-outlined block
- canvas.create_rectangle('6c 10c 9c 15c', outline='',
- stipple='gray25', fill=BLUE,
- tags=('item', ))
- def _draw_ovals():
- canvas.create_text('15c 8.2c', text='Ovals', anchor=N)
- # empty circle
- canvas.create_oval('11c 9.5c 14c 12.5c', outline=RED,
- width='3m', tags=('item', ))
- # filled, outlined oval
- canvas.create_oval('10.5c 13.5c 14.5c 15.5c', fill=GREEN,
- tags=('item', ))
- # stipple filled, non-outlined oval
- # Note: the stipple does not work under Windows
- # (Grayson, p289)
- canvas.create_oval('16c 10c 19c 15c', outline='',
- fill=BLUE, stipple='gray25',
- tags=('item', ))
- def _draw_text():
- canvas.create_text('25c 8.2c', text='Text', anchor=N)
- canvas.create_rectangle('22.4c 8.9c 22.6c 9.1c')
- txt = ["A short string of text, word-wrapped, justified left, ",
- "and anchored north (at the top). The rectangles show ",
- "the anchor points for each piece of text."]
- canvas.create_text('22.5c 9c', anchor=N, font=FONT1, width='4c',
- text=''.join(txt), tags=('item', ))
- canvas.create_rectangle('25.4c 10.9c 25.6c 11.1c')
- txt = ["Several lines,\n each centered\n",
- "individually,\nand all anchored\nat the left edge."]
- canvas.create_text('25.5c 11c', anchor=W, font=FONT1,
- fill=BLUE, text=''.join(txt), tags=('item', ))
- # Note: the stipple does not work well under Windows,
- # you get a stippled box with no colour rather
- # than purely stippled text; also, it will not
- # drag cleanly. The Tk manual states:
- # "Note that stipples are not well supported on platforms
- # that do not use X11 as their drawing API."
- canvas.create_rectangle('24.9c 13.9c 25.1c 14.1c')
- canvas.create_text('25c 14c', font=FONT2, anchor=CENTER,
- fill=RED, stipple='gray50',
- text='Stippled characters', tags=('item', ))
- def _draw_arcs():
- canvas.create_text('5c 16.2c', text='Arcs', anchor=N)
- # ellipse with missing wedge
- canvas.create_arc('0.5c 17c 7c 20c', fill=GREEN,
- outline='black', start=45, extent=270,
- style='pieslice', tags=('item', ))
- # open-ended circle with stippled outline
- # Note: stipple does not work under Windows
- canvas.create_arc('6.5c 17c 9.5c 20c', width='4m', style='arc',
- outline=BLUE, start=135, extent=270,
- outlinestipple='gray25',
- tags=('item', ))
- # rounded pie-slice
- canvas.create_arc('0.5c 20c 9.5c 24c', width='4m', style='pieslice',
- fill='', outline=RED, start=225, extent=-90,
- tags=('item', ))
- # partial ellipse
- canvas.create_arc('5.5c 20.5c 9.5c 23.5c', width='4m', style='chord',
- fill=BLUE, outline='', start=30, extent=270,
- tags=('item', ))
- def _draw_bitmaps():
- return # ZZZ
- canvas.create_text('15c 16.2c', text='Bitmaps', anchor=N)
- canvas.create_bitmap('13c 20c', tags=('item', ),
- bitmap='@images/face.xbm')
- canvas.create_bitmap('17c 18.5c', tags=('item', ),
- bitmap='@images/noletter.xbm')
- canvas.create_bitmap('17c 21.5c', tags=('item', ),
- bitmap='@images/letters.xbm')
- def _draw_windows():
- canvas.create_text('25c 16.2c', text='Windows', anchor=N)
- canvas.create_text('21c 17.9c', text='Button:', anchor='sw')
- btn = Button(canvas, text='Press Me', command=_button_press)
- canvas.create_window('21c 18c', window=btn, anchor='nw', tags=('item', ))
- canvas.create_text('21c 20.9c', text='Entry:', anchor='sw')
- entry = Entry(canvas, width=20)
- entry.insert(END, 'Edit this string')
- canvas.create_window('21c 21c', window=entry, anchor='nw', tags=('item', ))
- canvas.create_text('28.5c, 17.4c', text='Scale:', anchor=S)
- scale = Scale(canvas, from_=0, to=100, length='6c', orient=VERTICAL,
- command=_update_scale_value)
- canvas.create_text('27.5c, 17.4c', text='000', anchor=N, tags=('scalevalue',))
- canvas.create_window('28.5c 17.5c', window=scale, anchor=N, tags=('item', ))
- # ===================================================================================
- # Commands - the 'evt' object passed in is the originating widgets value or None,
- # not the full Event object generated by a bind
- # ===================================================================================
- def _button_press():
- # display some text, wait a few seconds, then remove it
- item = canvas.canvas.create_text('25c 18.1c', text='Ouch!!',
- fill='red', anchor=N)
- canvas.canvas.after(500,canvas.delete, item)
- def _update_scale_value(evt):
- # update value as slider is moved
- value = int(float(evt))
- canvas.itemconfigure('scalevalue', text='{:>3}'.format(value))
- # ===================================================================================
- # Bindings
- # ===================================================================================
- def _add_bindings():
- # behaviour confined to canvas objects with 'item' tag
- canvas.tag_bind('item', '<Any-Enter>',_item_enter)
- canvas.tag_bind('item', '<Any-Leave>',_item_leave)
- canvas.tag_bind('item', '<1>',_item_start_drag)
- canvas.tag_bind('item', '<B1-Motion>',_item_drag)
- # ===================================================================================
- # Bound methods
- # ===================================================================================
- def _item_enter(evt):
- # highlight the item under the mouse pointer
- canvas.__restoreItem = None # ID of item to be restored
- canvas.__restoreOpts = None # item options to be restored
- itemType = canvas.type(CURRENT) # current object's type
- if itemType == 'window': return # ignore windows
- canvas.__restoreItem = canvas.find_withtag('current')
- opt = '' # option to be used for highlight
- if itemType == 'bitmap':
- bg = canvas.itemconfigure(CURRENT, 'background')[4]
- opt = 'background'
- canvas.__restoreOpts = {opt: bg}
- else:
- fill = canvas.itemconfigure(CURRENT, 'fill')[4]
- # if the item has no fill, highlight it's outline
- if not fill and itemType in ('rectangle', 'arc', 'oval'):
- outline = canvas.itemconfigure(CURRENT, 'outline')[4]
- opt = 'outline'
- canvas.__restoreOpts = {opt: outline}
- else: # use it's fill option for the highlight
- opt = 'fill'
- canvas.__restoreOpts = {opt: fill}
- # apply highlight colour to current item
- canvas.itemconfigure(CURRENT, {opt: HIGHLIGHT})
- def _item_leave(evt):
- # remove highlight from item
- if canvas.__restoreItem == canvas.find_withtag(CURRENT):
- canvas.itemconfigure(canvas.__restoreItem, canvas.__restoreOpts)
- # Note: under Windows, the stippled text item will not
- # drag cleanly. The Tk manual states:
- # "Note that stipples are not well supported on platforms
- # that do not use X11 as their drawing API."
- def _item_start_drag(evt):
- # save drag start coordinates
- canvas.__lastX = canvas.canvasx(evt.x)
- canvas.__lastY = canvas.canvasy(evt.y)
- def _item_drag(evt):
- # the item moves (is dragged) with the mouse
- x = canvas.canvasx(evt.x)
- y = canvas.canvasy(evt.y)
- canvas.move(CURRENT, x-canvas.__lastX, y-canvas.__lastY)
- canvas.__lastX = x
- canvas.__lastY = y
- root=Tk()
- root.wm_attributes("-fullscreen", True)
- canvas=Canvas(root)
- # define colours
- BLUE = 'DeepSkyBlue'
- RED = 'red'
- BISQUE = 'bisque3'
- GREEN = 'SeaGreen3'
- HIGHLIGHT = 'SteelBlue2'
- # define fonts
- FONT1 = ('Helv', 12)
- FONT2 = ('Helv', 24, 'bold')
- root.title('Canvas Items Demo')
- canvas.isapp = True
- canvas.pack(expand=Y, fill=BOTH)
- _create_demo_panel()
- _create_main_scrollbars()
- root.bind("<Escape>", lambda x: root.destroy())
- root.mainloop()
- print 'Exited "Tk_canvasitems.py"'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement