Save text from Tk widget?

Steve Holden sholden at holdenweb.com
Mon Oct 23 07:16:45 EDT 2000


Steve wrote:
> 
> I have a function inside my main class to open a file in a ScrolledText
> widget.
> The default Windoze key bindings work for simple text editing, so I
> added
> a save function, but I declare a global variable to get the widget
> contents to the save function.  There must be a better way?
> 

Make zd an instance variable instead: this even allows you to have
multiple instances which won't interfere with each other.

        def viewDict(self):
                new = Toplevel()
                self.zd = ScrolledText(new, height = 30, width = 70)
                self.zd.pack(fill=BOTH, expand=YES)
                self.zd.insert('0.0', open('c:/mytools/mydict.py', 'r').read())
                Button(new, text='Save', fg='red',
command=self.saveDict).pack()

        def saveDict(self):
                contents = self.zd.get(1.0, END)
                output = open('c:/mytools/mydict.py', 'w')
                output.write(contents)
                output.close()

regards
 Steve
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/





More information about the Python-list mailing list