#!/usr/bin/env python
# This is a Python/Tk toolbar to run the Python demos.  Perhpas
# eventually we will be able to have plframe in a Python/Tk context. 

from Tkinter import *
import string

class PyDemo(Frame):
    def __init__(self, master=None):
	Frame.__init__(self,master)
	self.create_demo_bar()
	self.pack()

    def create_demo_bar(s):
	# Build a button for each PLplot python example program.

	s.x01 = Button( s, {
		"text" : "Example 1",
		"command" : s.x01m
		} )
	s.x02 = Button( s, {
		"text" : "Example 2",
		"command" : s.x02m
		} )
	s.x03 = Button( s, {
		"text" : "Example 3",
		"command" : s.x03m
		} )
	s.x04 = Button( s, {
		"text" : "Example 4",
		"command" : s.x04m
		} )
	s.x05 = Button( s, {
		"text" : "Example 5",
		"command" : s.x05m
		} )
	s.x06 = Button( s, {
		"text" : "Example 6",
		"command" : s.x06m
		} )
	s.x07 = Button( s, {
		"text" : "Example 7",
		"command" : s.x07m
		} )
	s.x08 = Button( s, {
		"text" : "Example 8",
		"command" : s.x08m
		} )
	s.x09 = Button( s, {
		"text" : "Example 9",
		"command" : s.x09m
		} )
	s.x10 = Button( s, {
		"text" : "Example 10",
		"command" : s.x10m
		} )
	s.x11 = Button( s, {
		"text" : "Example 11",
		"command" : s.x11m
		} )
	s.x12 = Button( s, {
		"text" : "Example 12",
		"command" : s.x12m
		} )
	s.x13 = Button( s, {
		"text" : "Example 13",
		"command" : s.x13m
		} )
	s.x14 = Button( s, {
		"text" : "Example 14",
		"command" : s.x14m
		} )
	s.x15 = Button( s, {
		"text" : "Example 15",
		"command" : s.x15m
		} )
	s.x16 = Button( s, {
		"text" : "Example 16",
		"command" : s.x16m
		} )
	s.x17 = Button( s, {
		"text" : "Example 17",
		"command" : s.x17m
		} )
	s.x18 = Button( s, {
		"text" : "Example 18",
		"command" : s.x18m
		} )
	s.x19 = Button( s, {
		"text" : "Example 19",
		"command" : s.x19m
		} )

	# Now pack them all.

	s.x01.pack()
	s.x02.pack()
	s.x03.pack()
	s.x04.pack()
	s.x05.pack()
	s.x06.pack()
	s.x07.pack()
	s.x08.pack()
	s.x09.pack()
	s.x10.pack()
	s.x11.pack()
	s.x12.pack()
	s.x13.pack()
	s.x14.pack()
	s.x15.pack()
	s.x16.pack()
	s.x17.pack()
	s.x18.pack()
	s.x19.pack()

# It sure seems to me that it ought to have been possible to do this
# dynamically, but I couldn't get it to work.  The following was my
# feeble effort, but it failed b/c I could never figure out how to get
# the command action to bind to the method I was trying to define in
# this class.  Very annoying.

##	for i in range(2,5):
##	    n = string.zfill( `i`, 2 )
##	    print i, "fn will be x" + n + "m"
##	    exec( "def x"+n+"m(s): import x"+n+"; x"+n+".main()",
##		  globals(), locals() )
##
####	    exec( """
####    def x""" + n + """m(s):
####	print "xxx" """ )
##	    
##	    exec( "s.x" + n + " = Button( s, {" +
##		  "'text' : 'Example " + `i` + "'," +	
##		  "'command' : s.x" + n + "m" +
##		  "} )", globals(), locals() )
##	    exec( "s.x"+ n +".pack()" )
##
####	    exec( "s.x"+`i`+" = Button(s, {" +
####		  "'text' : 'Example " + `i` + "'," +
####		  "'command' : s.x"+`i` + "m" +
####		  "} )" )
##
####	    exec( "def x" + `i` +"m(s):
####	    import x" + 

	# Don't forget the quit button!

	s.quit = Button( s, {
		"text" : "Quit",
		"command" : s.quit
		} )
	s.quit.pack()

# Build some methods for all the button actions, which run the
# corresponding demo.  Unfortunately, this isn't really doing what I
# want.  What I want si to run the demo.  If I use execfile, there si
# some nonsense about it not running in the right namespace or
# something.  I can't understand.  If I use import, they work, but on
# second invocations nothing is done.  If I add an explicit call to
# x01.main() here, then it runs twice the first time, once
# thereafter.  Can't win.  Obviously there is something I don't
# understand about the Python execution model.  Probably the
# difficulties with dynamically defining the methods are all bundled
# up in the same problem here.

    def x01m(s): import x01
    def x02m(s): import x02
    def x03m(s): import x03
    def x04m(s): import x04
    def x05m(s): import x05
    def x06m(s): import x06
    def x07m(s): import x07
    def x08m(s): import x08
    def x09m(s): import x09
    def x10m(s): import x10
    def x11m(s): import x11
    def x12m(s): import x12
    def x13m(s): import x13
    def x14m(s): import x14
    def x15m(s): import x15
    def x16m(s): import x16
    def x17m(s): import x17
    def x18m(s): import x18
    def x19m(s): import x19

demoapp = PyDemo()
demoapp.mainloop()
