Advertisement
here2share

# Tk_canvasitems.py ZZZ

Oct 12th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.73 KB | None | 0 0
  1. # Tk_canvasitems.py ZZZ
  2.  
  3. from Tkinter import *
  4.  
  5. def _create_widgets(): ###ZZZ
  6.     if canvas.isapp:
  7.         txt = ( "This window contains a canvas widget with examples of the ",
  8.                 "various kinds of items supported by canvases.  The following ",
  9.                 "operations are supported:\n",
  10.                 "\tItem Highlight:\titem under mouse pointer is highlighted.\n"
  11.                 "\tButton-1 drag:\tmoves item under pointer.")
  12.     canvas.create_text('22.5c 9c', anchor=N, font=FONT1, width='4c',
  13.                 text=''.join(txt), tags=('item', ))
  14.      
  15. def _create_demo_panel():
  16.      
  17.     # draw items
  18.     _draw_lines()
  19.     _draw_curves()
  20.     _draw_polygons()
  21.     _draw_rectangles()
  22.     _draw_ovals()
  23.     _draw_text()
  24.     _draw_arcs()
  25.     _draw_bitmaps()
  26.     _draw_windows()
  27.      
  28.     _add_bindings()
  29.  
  30. def _create_main_scrollbars():
  31.    
  32.     canvas.config(scrollregion=(0,0,1800, 1400), width='15c',
  33.                height='10c', relief=SUNKEN, borderwidth=2)
  34.     sbarV = Scrollbar(canvas)
  35.     sbarV.config(command=canvas.yview)                  
  36.     canvas.config(yscrollcommand=sbarV.set)              
  37.     sbarV.pack(side=RIGHT, fill=Y)
  38.  
  39.     sbarH = Scrollbar(canvas, orient=HORIZONTAL)
  40.     sbarH.config(command=canvas.xview)                  
  41.     canvas.config(xscrollcommand=sbarH.set)              
  42.     sbarH.pack(side=BOTTOM, fill=X)
  43.  
  44. # ===================================================================================
  45. # Canvas item drawing routines
  46. # ===================================================================================
  47. def _draw_lines():
  48.    
  49.     # Draw a 3x3 rectangular grid.
  50.     canvas.create_rectangle('0c 0c 30c 24c', width=2)
  51.     canvas.create_line('0c 8c 30c 8c', width=2)
  52.     canvas.create_line('0c 16c 30c 16c', width=2)
  53.     canvas.create_line('10c 0c 10c 24c', width=2)
  54.     canvas.create_line('20c 0c 20c 24c', width=2)
  55.      
  56.     canvas.create_text('5c .2c', text='Lines', anchor=N)
  57.     canvas.create_line('1c 1c 3c 1c 1c 4c 3c 4c', width='2m',
  58.                 fill=BLUE, cap='butt', join='miter',
  59.                 tags=('item', ))  # Z
  60.     canvas.create_line('4.67c 1c 4.67c 4c', arrow=LAST,
  61.                 tags=('item', ))  # line w/one arrow
  62.     canvas.create_line('6.33c 1c 6.33c 4c', arrow=BOTH,
  63.                 tags=('item'))  # line with two arrows
  64.  
  65.     # maze
  66.     line = ['5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c ',
  67.             '8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c']
  68.     canvas.create_line(''.join(line), width=3, fill=RED, tags=('item', ))
  69.      
  70.     # stippled line
  71.     canvas.create_line('1c 5c 7c 5c 7c 7c 9c 7c', width='.5c', stipple='gray25',
  72.                 arrow=BOTH, arrowshape=(15, 15, 7), tags=('item', ))
  73.  
  74.     # 'M' shape
  75.     canvas.create_line('1c 7c 1.75c 5.8c 2.5c 7cc 3.25c 5.8c 4c 7c',
  76.                 width='.5c', cap=ROUND, join=ROUND, tags=('item', ))
  77.      
  78. def _draw_curves():
  79.      
  80.     canvas.create_text('15c .2c', text='Curves (smoothed lines)', anchor=N)
  81.      
  82.     # curved
  83.     canvas.create_line('11c 4c 11.5c 1c 13.5c 1c 14c 4c', smooth='on',
  84.                 fill=BLUE, tags=('item', ))
  85.      
  86.     # snake with arrows
  87.     canvas.create_line('15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c',
  88.                 smooth='on', arrow=BOTH, width=3, tags=('item',))
  89.      
  90.     # stippled infinity
  91.     line = ['12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c ',
  92.             '16.5c 4.5c 13.5c 7.5c 12c 6c']
  93.     canvas.create_line(''.join(line), smooth='on', width='3m', stipple='gray25',
  94.                 cap=ROUND, fill=RED, tags=('item', ))      
  95.  
  96.  
  97. def _draw_polygons():
  98.      
  99.     canvas.create_text('25c .2c', text='Polygons', anchor=N)
  100.      
  101.     # 4 pointed star
  102.     poly = ['21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c ',
  103.             '24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c']
  104.     canvas.create_polygon(''.join(poly), fill=GREEN,
  105.                      outline='black', width=4, tags=('item', ))
  106.  
  107.     # roller-coaster
  108.     poly = ['25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c ',
  109.             '29c 1c 29c 4c 29c 4c']
  110.     canvas.create_polygon(''.join(poly), fill=RED, smooth='on',
  111.                      tags=('item', ))
  112.  
  113.     # stippled blocks
  114.     poly = ['22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c ',
  115.             '28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c ']
  116.     canvas.create_polygon(''.join(poly), stipple='gray25',
  117.                      outline='black', tags=('item', ))
  118.      
  119.  
  120. def _draw_rectangles():
  121.      
  122.     canvas.create_text('5c 8.2c', text='Rectangles', anchor=N)
  123.      
  124.     # empty square
  125.     canvas.create_rectangle('1c 9.5c 4c 12.5c', outline=RED,
  126.                      width='3m', tags=('item', ))
  127.  
  128.     # filled, outlined block
  129.     canvas.create_rectangle('0.5c 13.5c 4.5c 15.5c', fill=GREEN,
  130.                      tags=('item', ))
  131.      
  132.     # stipple filled, non-outlined block
  133.     canvas.create_rectangle('6c 10c 9c 15c', outline='',
  134.                      stipple='gray25', fill=BLUE,
  135.                      tags=('item', ))
  136.      
  137. def _draw_ovals():
  138.      
  139.     canvas.create_text('15c 8.2c', text='Ovals', anchor=N)
  140.      
  141.     # empty circle
  142.     canvas.create_oval('11c 9.5c 14c 12.5c', outline=RED,
  143.                 width='3m', tags=('item', ))
  144.  
  145.     # filled, outlined oval
  146.     canvas.create_oval('10.5c 13.5c 14.5c 15.5c', fill=GREEN,
  147.                 tags=('item', ))
  148.  
  149.     # stipple filled, non-outlined oval
  150.     # Note: the stipple does not work under Windows
  151.     #      (Grayson, p289)
  152.     canvas.create_oval('16c 10c 19c 15c', outline='',
  153.                 fill=BLUE, stipple='gray25',
  154.                 tags=('item', ))
  155.  
  156. def _draw_text():
  157.      
  158.     canvas.create_text('25c 8.2c', text='Text', anchor=N)
  159.     canvas.create_rectangle('22.4c 8.9c 22.6c 9.1c')
  160.     txt = ["A short string of text, word-wrapped, justified left, ",
  161.            "and anchored north (at the top).  The rectangles show ",
  162.            "the anchor points for each piece of text."]
  163.     canvas.create_text('22.5c 9c', anchor=N, font=FONT1, width='4c',
  164.                 text=''.join(txt), tags=('item', ))
  165.  
  166.     canvas.create_rectangle('25.4c 10.9c 25.6c 11.1c')
  167.     txt = ["Several lines,\n each centered\n",
  168.            "individually,\nand all anchored\nat the left edge."]
  169.     canvas.create_text('25.5c 11c', anchor=W, font=FONT1,
  170.                 fill=BLUE, text=''.join(txt), tags=('item', ))
  171.  
  172.     # Note: the stipple does not work well under Windows,
  173.     #      you get a stippled box with no colour rather
  174.     #      than purely stippled text; also, it will not
  175.     #      drag cleanly. The Tk manual states:
  176.     #       "Note that stipples are not well supported on platforms
  177.     #        that do not use X11 as their drawing API."
  178.     canvas.create_rectangle('24.9c 13.9c 25.1c 14.1c')     
  179.     canvas.create_text('25c 14c', font=FONT2, anchor=CENTER,
  180.                 fill=RED, stipple='gray50',
  181.                 text='Stippled characters', tags=('item', ))
  182.  
  183. def _draw_arcs():
  184.      
  185.     canvas.create_text('5c 16.2c', text='Arcs', anchor=N)
  186.      
  187.     # ellipse with missing wedge
  188.     canvas.create_arc('0.5c 17c 7c 20c', fill=GREEN,
  189.                  outline='black', start=45, extent=270,
  190.                  style='pieslice', tags=('item', ))
  191.  
  192.     # open-ended circle with stippled outline
  193.     # Note: stipple does not work under Windows
  194.     canvas.create_arc('6.5c 17c 9.5c 20c', width='4m', style='arc',
  195.                  outline=BLUE, start=135, extent=270,
  196.                  outlinestipple='gray25',
  197.                  tags=('item', ))
  198.  
  199.     # rounded pie-slice
  200.     canvas.create_arc('0.5c 20c 9.5c 24c', width='4m', style='pieslice',
  201.                  fill='', outline=RED, start=225, extent=-90,
  202.                  tags=('item', ))
  203.  
  204.     # partial ellipse
  205.     canvas.create_arc('5.5c 20.5c 9.5c 23.5c', width='4m', style='chord',
  206.                  fill=BLUE, outline='', start=30, extent=270,
  207.                  tags=('item', ))
  208.      
  209.  
  210. def _draw_bitmaps():
  211.  
  212.     return # ZZZ
  213.     canvas.create_text('15c 16.2c', text='Bitmaps', anchor=N)
  214.      
  215.     canvas.create_bitmap('13c 20c', tags=('item', ),
  216.                     bitmap='@images/face.xbm')
  217.      
  218.     canvas.create_bitmap('17c 18.5c', tags=('item', ),
  219.                     bitmap='@images/noletter.xbm')
  220.  
  221.     canvas.create_bitmap('17c 21.5c', tags=('item', ),
  222.                     bitmap='@images/letters.xbm')
  223.      
  224.  
  225. def _draw_windows():
  226.  
  227.     canvas.create_text('25c 16.2c', text='Windows', anchor=N)
  228.     canvas.create_text('21c 17.9c', text='Button:', anchor='sw')
  229.     btn = Button(canvas, text='Press Me', command=_button_press)
  230.     canvas.create_window('21c 18c', window=btn, anchor='nw', tags=('item', ))
  231.  
  232.     canvas.create_text('21c 20.9c', text='Entry:', anchor='sw')
  233.     entry = Entry(canvas, width=20)
  234.     entry.insert(END, 'Edit this string')
  235.     canvas.create_window('21c 21c', window=entry, anchor='nw', tags=('item', ))
  236.      
  237.     canvas.create_text('28.5c, 17.4c', text='Scale:', anchor=S)
  238.     scale = Scale(canvas, from_=0, to=100, length='6c', orient=VERTICAL,
  239.               command=_update_scale_value)
  240.     canvas.create_text('27.5c, 17.4c', text='000', anchor=N, tags=('scalevalue',))
  241.     canvas.create_window('28.5c 17.5c', window=scale, anchor=N, tags=('item', ))
  242.    
  243. # ===================================================================================
  244. # Commands - the 'evt' object passed in is the originating widgets value or None,
  245. #           not the full Event object generated by a bind
  246. # ===================================================================================
  247. def _button_press():
  248.     # display some text, wait a few seconds, then remove it
  249.     item = canvas.canvas.create_text('25c 18.1c', text='Ouch!!',
  250.                                  fill='red', anchor=N)
  251.     canvas.canvas.after(500,canvas.delete, item)
  252.              
  253.      
  254. def _update_scale_value(evt):
  255.     # update value as slider is moved
  256.     value = int(float(evt))
  257.     canvas.itemconfigure('scalevalue', text='{:>3}'.format(value))
  258.  
  259.  
  260. # ===================================================================================
  261. # Bindings
  262. # ===================================================================================
  263. def _add_bindings():
  264.  
  265.     # behaviour confined to canvas objects with 'item' tag
  266.     canvas.tag_bind('item', '<Any-Enter>',_item_enter)
  267.     canvas.tag_bind('item', '<Any-Leave>',_item_leave)
  268.  
  269.     canvas.tag_bind('item', '<1>',_item_start_drag)
  270.     canvas.tag_bind('item', '<B1-Motion>',_item_drag)
  271.  
  272.  
  273. # ===================================================================================
  274. # Bound methods
  275. # ===================================================================================
  276. def _item_enter(evt):
  277.     # highlight the item under the mouse pointer
  278.     canvas.__restoreItem = None # ID of item to be restored
  279.     canvas.__restoreOpts = None # item options to be restored
  280.      
  281.     itemType = canvas.type(CURRENT)   # current object's type
  282.     if itemType == 'window': return # ignore windows
  283.      
  284.     canvas.__restoreItem = canvas.find_withtag('current')
  285.     opt = ''    # option to be used for highlight
  286.      
  287.     if itemType == 'bitmap':
  288.         bg = canvas.itemconfigure(CURRENT, 'background')[4]
  289.         opt = 'background'
  290.         canvas.__restoreOpts = {opt: bg}
  291.     else:
  292.         fill = canvas.itemconfigure(CURRENT, 'fill')[4]
  293.          
  294.         # if the item has no fill, highlight it's outline
  295.         if not fill and itemType in ('rectangle', 'arc', 'oval'):
  296.             outline = canvas.itemconfigure(CURRENT, 'outline')[4]
  297.             opt = 'outline'
  298.             canvas.__restoreOpts = {opt: outline}
  299.         else:   # use it's fill option for the highlight
  300.             opt = 'fill'
  301.             canvas.__restoreOpts = {opt: fill}
  302.  
  303.     # apply highlight colour to current item
  304.     canvas.itemconfigure(CURRENT, {opt: HIGHLIGHT})
  305.  
  306. def _item_leave(evt):
  307.     # remove highlight from item
  308.  
  309.     if canvas.__restoreItem == canvas.find_withtag(CURRENT):
  310.         canvas.itemconfigure(canvas.__restoreItem, canvas.__restoreOpts)
  311.  
  312. # Note: under Windows, the stippled text item will not
  313. #      drag cleanly. The Tk manual states:
  314. #        "Note that stipples are not well supported on platforms
  315. #         that do not use X11 as their drawing API."
  316. def _item_start_drag(evt):
  317.     # save drag start coordinates
  318.      
  319.     canvas.__lastX = canvas.canvasx(evt.x)
  320.     canvas.__lastY = canvas.canvasy(evt.y)
  321.              
  322. def _item_drag(evt):
  323.     # the item moves (is dragged) with the mouse
  324.      
  325.     x = canvas.canvasx(evt.x)
  326.     y = canvas.canvasy(evt.y)
  327.     canvas.move(CURRENT, x-canvas.__lastX, y-canvas.__lastY)
  328.     canvas.__lastX = x
  329.     canvas.__lastY = y
  330.  
  331. root=Tk()
  332. root.wm_attributes("-fullscreen", True)
  333.  
  334. canvas=Canvas(root)
  335.  
  336. # define colours
  337. BLUE = 'DeepSkyBlue'
  338. RED = 'red'
  339. BISQUE = 'bisque3'
  340. GREEN = 'SeaGreen3'
  341. HIGHLIGHT = 'SteelBlue2'
  342.  
  343. # define fonts
  344. FONT1 = ('Helv', 12)
  345. FONT2 = ('Helv', 24, 'bold')
  346.  
  347. root.title('Canvas Items Demo')
  348. canvas.isapp = True
  349.  
  350. canvas.pack(expand=Y, fill=BOTH)
  351.  
  352. _create_demo_panel()
  353. _create_main_scrollbars()
  354. root.bind("<Escape>", lambda x: root.destroy())
  355.  
  356. root.mainloop()
  357.  
  358. print 'Exited "Tk_canvasitems.py"'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement