Re: tk obj file into tkinter

Guido.van.Rossum@cwi.nl
Wed, 06 Jul 1994 09:56:20 +0200

> Since I don't have tkinter, can someone out there see if tkinter will
> interpret and draw this tk object file? I'm debating using tcl/tk or
> tkinter for the display and manipulation of files in this format.
> (I'd prefer to use tkinter, if possible.)

Here's a Tkinter program that reads your file line by line and renders
it. Note that since your file is in Tcl syxtax, it just passes its
lines to the Tcl interpreter by calling the 'eval' function. The only
tricky bit is setting the 'name' attributes of the frame and canvas to
'c' so the canvas is actually called '.c.c' (by default Tk widgets
created by Tkinter have random digit strings as names).

========================================================================
# Test program for Mike Tibbs' query

from Tkinter import *

def main():
# Open the file (no point in continuing if it isn't there)
fp = open('mech.tkobj', 'r')

# Create the widget structure -- the canvas is called ".c.c"
root = Tk()
root.minsize(1, 1)
f = Frame(root, {'name': 'c',
Pack: {'expand': 1, 'fill': 'both'}})
c = Canvas(f, {'name': 'c', 'width': '11i', 'height': '7i',
Pack: {'expand': 1, 'fill': 'both'}})

# Read the file, passing each line to the Tck interpreter

while 1:
line = fp.readline()
if not line: break
root.tk.call('eval', line)

# Start processing events (i.e. view the picture)

root.mainloop()

main()
========================================================================

I've compared its output with the GIF you sent and the only difference
is that the GIF is smaller -- this is because the pixels on my screen
are smaller which affects the translation from inches to pixels.

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>