[Tutor] More Tkinter Help Please [callbacks and embedded
functions]
SA
sarmstrong13@mac.com
Mon, 17 Jun 2002 16:06:23 -0500
Ok.
Here's the code:
from Tkinter import *
import os
import commands
import tkSimpleDialog
class PyShell:
def saving_callback(): Saving(f)
def clearin(self):
self.t1.delete(0.0,END)
def clearout(self):
self.t2.delete(0.0,END)
def expyth(self):
self.t2.delete(0.0, END)
self.output = commands.getoutput(self.t1.get(0.0, END))
self.t2.insert(END, self.output)
def __init__(self, top):
self.t1 = Text(top, height="12", width="84", font="Courier 12")
self.t1.pack(side=TOP, pady=2)
f = Frame(top)
f.pack()
self.b1 = Button(f, text="Execute", command=self.expyth)
self.b1.pack(side=LEFT)
self.b2 = Button(f, text="Clear Input", command=self.clearin)
self.b2.pack(side=LEFT)
self.b3 = Button(f, text="Clear Output", command=self.clearout)
self.b3.pack(side=LEFT)
self.b4 = Button(f, text="Save Input", command=self.saving_callback)
self.b4.pack(side=LEFT)
self.t2 = Text(top, height="12", width="84", bg="lightblue",
font="Courier 12")
self.t2.pack(side=TOP, pady=2)
class Saving(tkSimpleDialog.Dialog):
def savin(self, question):
Label(question, text="Directory:").grid(row=0)
Label(question, text="Filename:").grid(row=1)
self.e1 = Entry(question)
self.e2 = Entry(question)
self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
def apply(self):
self.dir = self.e1.get()
self.fn = self.e2.get()
self.sav = self.dir + self.fn
self.savfile = open(self.sav, "w")
for line in self.t1.readlines():
self.savefile.write(line)
self.savefile.close()
root = Tk()
app = PyShell(root)
root.mainloop()
Now if I move def saving_callback(): Saving(f) within __init__, I get two
same windows when I launch the app. I then click the Save button, a Dialog
box pops up , but only with the OK and Cancel buttons and no entries. When I
click OK I get the following error:
Traceback (most recent call last):
File "/sw/src/root-python-2.2.1-6/sw/lib/python2.2/lib-tk/Tkinter.py",
line 1292, in __call__
return apply(self.func, args)
File
"/sw/src/root-python-2.2.1-6/sw/lib/python2.2/lib-tk/tkSimpleDialog.py",
line 126, in ok
self.apply()
File "PyShell.py", line 41, in apply
self.dir = self.e1.get()
AttributeError: Saving instance has no attribute 'e1'
I'm assuming this is because the dialogbox is popping up without the entry
fields so there is no value to get. I think the problem may be solved by
using Toplevel, but I will have to investigate this further so that I
understand how to apply the Toplevel widget.
Thanks in advance.
SA