[Tutor] Cross-class data sharing...

Roger Merchberger zmerch@30below.com
Tue Apr 8 20:56:01 2003


... My name is Roger... and I'm a newbie...

[[ Is there a 12-step program for this??? :-O ]]

Well, only a newbie to Python, and OOP programming. Anyway, I'm reading 
_Programming Python_ and lemme tell ya, it's a good book, but it's a 
"thick" read! As a programming practice, I've been working with Tkinter 
creating entry fields & action buttons for an MPEG Bitrate calculator, 
where it takes the file path from one frame, and the filename from another 
frame. Clicking a button puts the fields together, opens a file, and writes 
a small script, code to follow.

The 2 frames in question are set up as classes, and everything seems to 
work [kinda] until it comes to the cross-class Entry data fields... After 
futzing with this for 3 hours last nite, I realized that I don't think my 
book covered that yet...

I could prolly hack it up without classes, but a) I wanted to see if I 
could do it, and b) it would be nice to have the code reuse ability of 
classes for future programs.

Any pointers to help me along my path? Thanks to one and all!

Roger "Merch" Merchberger

=-=-=-=-=-=-= snip here -- code follows =-=-=-=-=-=-=-=

#!/usr/bin/python

# This is my first attempt at a fromthebrain gui proggie...

from Tkinter import *
import sys

class zfile(Frame):
   def __init__(self,master=None):
     Frame.__init__(self,master)
     Pack.config(self)
     zlabel=Label(self)
     zlabel.config(text="Show Prepend Text",width=20)
     zlabel.pack(side=LEFT)
     self.zshow=StringVar()
     zshowent=Entry(self)
     zshowent.config(width=50)
     zshowent.config(textvariable=self.zshow)
     self.zshow.set('')
     zshowent.pack(side=LEFT)
   def state(self):
     return self.zshow.get()


class get_path(Frame):
   def __init__(self,master=None,zfile=''):
     Frame.__init__(self,master)
     Pack.config(self)
     avilabel=Label(self)
     avilabel.config(text="AviScript Path",width=20)
     avilabel.pack(side=LEFT)
     self.avipath=StringVar()
     avient=Entry(self)
     avient.config(width=50)
     avient.config(textvariable=self.avipath)
     self.avipath.set('h:/!tivo/bork.avs')
     avient.pack(side=LEFT)
   def state(self):
     return self.avipath.get()

def wrtfile(fname,addstring):
   fileout = open(fname,"w")
   zout = [ '#  bork... to see if it can append...\n' ]

   d2v='vid=MPEG2Source("%s.d2v")\n' % addstring
   wav='aud=WAVSource("%s.wav")\n' % addstring

   therest = [ 'AudioDub(vid,aud)\n',
               'DelayAudio(.0xx)\n',
               'src=ResampleAudio(44100)\n',
               '\n',
               'src\n' ]

   zout.append(d2v)
   zout.append(wav)

   for bing in therest:
     zout.append(bing)

   fileout.writelines(zout)


# now for the main loop...

if __name__ == '__main__':
   root=Tk()

   fname=zfile(root)
   addstr=get_path(root)

   dz=Frame(root)
   aviout=Button(dz,text="AviOut",command=wrtfile(fname,addstr))
   aviout.pack(side=RIGHT)
   aviout=Button(dz,text="Die",command=root.destroy)
   aviout.pack(side=RIGHT)
   dz.pack()

#  root.mainloop()