canvassing for assistance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean McIlroy

    #1

    canvassing for assistance

    Hi all!

    I've written a utility for making diagrams. It could also be a good
    environment for experimenting with a Tk canvas, so I'm including the
    code here (see below). The problem is that, when I save a canvas and
    include the resulting postscript file in a LaTeX document, I often
    find that the right edge of the canvas has been cut off and/or that
    there's a bunch of extra space at the bottom that forces the picture
    to take up a whole page just by itself. The Introduction to Tkinter
    lists a bunch of options for the Canvas postscript method, but it
    doesn't say anything about the semantics of any of them, and there are
    several that sound like they could be what I need. So, if anybody
    knows how to exercise finer control over the Canvas postscript method,
    I'd be grateful to hear about it.

    Peace,
    STM


    ############### ############### ############### ##
    ## FRESH SHELL: import canvasser
    ## (so that text can be copied from the shell)
    ############### ############### ############### ##

    pencil = 1
    eraser = 10
    color = 'black'

    def save():
    from tkSimpleDialog import askstring
    filename = askstring('save diagram','enter name of diagram: ') +
    '.eps'
    canvas.postscri pt(file=filenam e,width=100,hei ght=100,pagewid th=100,pageheig ht=100)

    def circle(x,y,radi us=25,color=Non e):
    r = radius
    return canvas.create_o val(x-r,y-r,x+r,y+r,fill= color)

    ############### ############### ############### ############### ############### ######

    __P__ = None
    __I__ = None

    def draw(event):
    global __P__
    Q = [event.x,event.y]
    canvas.create_l ine(__P__[0],__P__[1],Q[0],Q[1],width=pencil,f ill=color)
    __P__ = Q

    def erase(event):
    r = eraser
    x,y = event.x,event.y
    for x in canvas.find_ove rlapping(x-r,y-r,x+r,y+r):
    canvas.delete(x )

    def carry(event):
    if __I__==None: return
    C = canvas.coords(_ _I__)
    x,y = event.x,event.y
    if len(C)==2: canvas.coords(_ _I__,x,y)
    else:
    a,b = C[:2]
    f = lambda i: ( i%2 and [y-b] or [x-a] ) [0]
    D = [x,y] + [C[i] + f(i) for i in range(2,len(C))]
    canvas.coords(_ _I__,*D)

    def scale(event):
    C = canvas.coords(_ _I__)
    if len(C)<>4: return
    canvas.coords(_ _I__,C[0],C[1],event.x,event. y)

    def point(event):
    codeArea.insert (INSERT,str(eve nt.x) + ',' + str(event.y))

    def item(event):
    codeArea.insert (INSERT,str(can vas.find_closes t(event.x,event .y)[0]))

    def mouseDown(event ):
    global __P__,__I__
    m = mode.get()
    if m==0: __P__ = [event.x,event.y]
    elif m==2: point(event)
    elif m==3: item(event)
    elif m>=4: __I__ = canvas.find_clo sest(event.x,ev ent.y)

    def mouseDrag(event ):
    m = mode.get()
    if m==0: draw(event)
    elif m==1: erase(event)
    elif m==4: carry(event)
    elif m==5: scale(event)

    def mouseUp(event):
    global __P__,__I__
    __P__ = None
    __I__ = None

    def runCode(dummy):
    x = codeArea.get()
    y = [i for i in range(len(x)) if x[i]=='=' and
    x[:i].count('(')==0]
    z = y and x[:y[0]] + '=' + x[y[0]+1:] or x
    print '>>> ' + z
    try: exec z in globals()
    except: print 'ERROR'
    codeArea.delete (0,END)

    from Tkinter import *
    print '*'*80
    print 'REMINDER: canvas; pencil,eraser,c olor; save,circle'
    print '*'*80
    root = Tk()
    canvas = Canvas(root,bac kground='white' )
    canvas.bind('<B utton-1>',mouseDown)
    canvas.bind('<B 1-Motion>',mouseD rag)
    canvas.bind('<B uttonRelease-1>',mouseUp)
    canvas.pack(sid e=TOP,expand=1, fill=BOTH)
    codeArea = Entry(root,font =6)
    codeArea.pack(s ide=TOP,expand= 1,fill=X)
    codeArea.bind(' <Return>',runCo de)
    ctrl = Frame(root)
    mode = IntVar()
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=0,text='d raw').pack(side =LEFT)
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=1,text='e rase').pack(sid e=LEFT)
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=2,text='p oint').pack(sid e=LEFT)
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=3,text='i tem').pack(side =LEFT)
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=4,text='c arry').pack(sid e=LEFT)
    Radiobutton(ctr l,indicatoron=0 ,variable=mode, value=5,text='s cale').pack(sid e=LEFT)
    ctrl.pack(side= TOP,pady=10)
    root.title('can vasser')
    root.mainloop()
  • stewart.midwinter@gmail.com

    #2
    Re: canvassing for assistance

    Sean, nice work on canvasser! One question: what is the purpose of
    'scale'? I notice that if you have already drawn a line on the canvas,
    then 'scale' can be used to draw a straight-line element extending from
    the end of the previous freehand line, but if you start with a blank
    screen, 'scale' has no effect.

    BTW if you want to extend your app further, take a look at paint.py in
    the Vaults of Parnassus:


    cheers,
    S

    Comment

    • Sean McIlroy

      #3
      Re: canvassing for assistance

      'scale' puts the lower-right corner of a bounding box where the
      pointer is, while keeping the upper-left corner where it was before
      (or, if the relevant item's coordinates aren't of bounding-box type,
      the function does nothing).

      Thanks for the link.

      Peace,
      STM


      "stewart.midwin ter@gmail.com" <stewart.midwin ter@gmail.com> wrote in message news:<110968813 0.019233.46530@ l41g2000cwc.goo glegroups.com>. ..[color=blue]
      > Sean, nice work on canvasser! One question: what is the purpose of
      > 'scale'? I notice that if you have already drawn a line on the canvas,
      > then 'scale' can be used to draw a straight-line element extending from
      > the end of the previous freehand line, but if you start with a blank
      > screen, 'scale' has no effect.
      >
      > BTW if you want to extend your app further, take a look at paint.py in
      > the Vaults of Parnassus:
      > http://py.vaults.ca/apyllo.py?i=173784088
      >
      > cheers,
      > S[/color]

      Comment

      Working...